2

I am doing a server side coding and wanted to convert the file extension to pdf extension and it should be saved in S3. Any suggestions will be valuable. I am a beginner in this area, so apologies if the question offends anyone.

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()
 });
 }
 };
AksharaDL
  • 63
  • 10

1 Answers1

0

Try by looking into this example. You probably would need to save the PDF on disk before uploading it to a bucket. Also try adding the X and Y position to where the image will be placed on the 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();   
});
felipenbrito
  • 70
  • 1
  • 9