Can a pdf file save images from a url?

I have a node.js application in which I use pdfkit to create PDF documents. I want to be able to include images from a URL in pdf. I cannot save the image in the file system because my runtime is read-only, and in the pdf set it seems to find the images to be inserted from the file system directory. Is there a way I can use url in pdf set to embed image?


Here . This guy modified pdfkit to include this functionality.

+6
source share
2 answers

PDFKit now supports passing buffers to the doc.image method instead of the file name. See this commit. That way, you can do as another answer suggests and download the image from the URL yourself, and then transfer the buffer directly to PDFKit instead of saving it to a file first.

+2
source

you can use http.get:

  http.get('YOUR URL TO GET THE IMAGE').on('response', function(res) res.setEncoding('binary'); res.on('data', function(chunk){ buffer += chunk; }); res.on('end', function(){ fs.writeFile('PATH TO SAVE IMAGE', buffer, 'binary', function (err) { if (err){ throw err; } doc = new PDFDocument({ size: 'LETTER or any other size pdfkit offer' }); doc.image('PATH TO SAVE IMAGE', 0, 0, { fit: [WIDTH, HEIGHT] }) .fontSize(10).text('text 1', 100, 170) .fontSize(16).text('text 2', 60, 120) }); //After file is download and was write into the HD will use it }).end(); //EO Downloading the file 
+1
source

Source: https://habr.com/ru/post/913000/


All Articles