如何使用nodejs中的PDFKIT保存PNG格式为PDF格式[包括代码]



我正在执行服务器端编码,并希望将文件扩展名转换为PDF扩展名,并且应该保存在S3中。任何建议都是有价值的。我是这个领域的初学者,如果问题得罪任何人,则很抱歉。

uploadfiles :( files,bucketname,path(=> {

 return new Promise(async function(resolve, reject) {
   var doc = new PDFDocument()
   var file = files[0];
      let filename = file.originalFilename;
      var file_path =  file.path;
      doc.image(file_path, {
       fit: [250, 300],
       align: 'center',
       valign: 'center'
   });
   //doc.y = 300
 //doc.pipe(res)
  //  console.log('img',img);
   var path = 'images/'+ file.originalFilename;
 console.log( 'path',path);
   var path = await uploadFiles(file, bucketName, path);
     resolve(path);
     //doc.end()
 });
 }
 };

尝试一下此示例。您可能需要将PDF保存在磁盘上,然后再将其上传到存储桶。还要尝试将X和Y位置添加到将图像放置在PDF上的位置。

router.route('/generate.pdf')
     .post(multiparty(), function(req,res,next){

 // Creates a new instace of a Pdf Document Model
 var pdfDocument = new PdfDocument(req.body);
 // New Instance of the PDFKit PDFDOCUMENT
 var pdf = new PDFDocument({
     size: 'LETTER',
     info: {
        Title: 'title',
         Author: 'me',
     }
 });
 // Top Logo
 pdf.image('./logo.jpg', 500, 35, {width: 60});
 // Document Top Title
 pdf.fontSize(14).text('Some Title', {
     align: 'center' });
 var fileName = 'some.pdf';
 // Stream contents to a file
 pdf.pipe(fs.createWriteStream(fileName))
    .on('finish', function () {
        console.log('PDF closed');
    });
    // Close PDF and write file.
    pdf.pipe(res);
    pdf.end();   
});

最新更新