pdf工具包可以保存网址中的图像吗?

jok*_*ham 6 pdfkit node.js

我有一个node.js应用程序,我在其中使用pdfkit生成pdf文档.我希望能够在PDF格式中包含来自网址的图片.我无法将图像保存到文件系统,因为我的运行时环境是只读的,pdf工具包似乎找到要从文件系统目录嵌入的图像.有没有办法在pdf工具包中使用url来嵌入图像?


在这里.这家伙修改了pdfkit以包含该功能.

Dor*_*gal 1

你可以使用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
Run Code Online (Sandbox Code Playgroud)