简体   繁体   English

如何在nodejs中使用pdfkit将png格式保存为pdf格式[包括代码]

[英]how to save png format to pdf format using pdfkit in nodejs [code included]

I am doing a server side coding and wanted to convert the file extension to pdf extension and it should be saved in S3. 我正在服务器端进行编码,希望将文件扩展名转换为pdf扩展名,并且应将其保存在S3中。 Any suggestions will be valuable. 任何建议都是有价值的。 I am a beginner in this area, so apologies if the question offends anyone. 我是这个领域的初学者,如果这个问题冒犯了任何人,那么我们深表歉意。

uploadFiles: (files, bucketName, path) =>{ uploadFiles :(文件,bucketName,路径)=> {

 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()
 });
 }
 };

Try by looking into this example. 通过查看此示例进行尝试。 You probably would need to save the PDF on disk before uploading it to a bucket. 在将PDF上载到存储桶之前,您可能需要将其保存在磁盘上。 Also try adding the X and Y position to where the image will be placed on the 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();   
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM