Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDFKit - locate image in center

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?

like image 648
griffon vulture Avatar asked Oct 22 '14 05:10

griffon vulture


People also ask

What is PDFKit?

Source Code PDFKit A JavaScript PDF generation library for Node and the browser. Description PDFKit is a PDF document generation library for Node and the browser that makes creating complex, multi-page, printable documents easy.

Why are my images not appearing in my generated PDF documents?

This can cause issues of Images not appearing in your generated PDF documents if they are not stored locally on the server. Manually insert into HTML template. Our front end is uploading images and sending them to our RAILS API to generate the HTML document

How do I get the output of PDFKit in HTML5?

Distributed as pdfkit.standalone.jsfile in the releasesor in the package jsfolder. In addition to PDFKit, you'll need somewhere to stream the output to. HTML5 has a Blobobject which can be used to store binary data, and get URLs to this data in order to display PDF output inside an iframe, or upload to a server, etc.

How to get a blob from the output of PDFKit?

HTML5 has a Blobobject which can be used to store binary data, and get URLs to this data in order to display PDF output inside an iframe, or upload to a server, etc. In order to get a Blob from the output of PDFKit, you can use the blob-streammodule.


2 Answers

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 )
like image 163
griffon vulture Avatar answered Oct 01 '22 08:10

griffon vulture


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'
});
like image 21
Ibrahim Zahoor Avatar answered Oct 01 '22 09:10

Ibrahim Zahoor