7

Using PDFKit on node.js:

 var PDFDocument = require('pdfkit')
 var doc = new PDFDocument()
 doc.image('images/test.jpeg')

How can I centerize an image added to the PDF?

Is it optional to do it using PDFKit or do I need to use another library?

griffon vulture
  • 6,594
  • 6
  • 36
  • 57

3 Answers3

20

I've found an indirect way to solve the problem - simply calculate the center and locate the picture there:

 doc.image('images/test.jpeg', (doc.page.width - imageWidth) /2 )
griffon vulture
  • 6,594
  • 6
  • 36
  • 57
16

Using PDFKit on node.js: We can center the image using following code

doc.image('path/to/image.png', {
  fit: [250, 300],
  align: 'center',
  valign: 'center'
});
Ibrahim Zahoor
  • 161
  • 1
  • 4
  • The `align` and `valign` in this example center the image in the rectangle defined by `fit` not in the page. – Prdufresne Aug 15 '22 at 20:10
1

You can use this, this code place your image at middle of the line

let imageWidth = 180 // what you wants
doc.image('path/to/image.png', 
        doc.page.width/2 - imageWidth/2,doc.y,{
        width:imageWidth
      });