RE : PDF Attachments || Attachment Create

Hi All

  1. I am creating a pdf using pdf.generatePdf(), which is creating fine as expected;
  2. I would like to save the resulting pdf to my existing object (they are relatively small pdfs), rather than uploading the same to S3;
  3. I have tried Attachment.create() but have run into a plethora of errors and issues.

Please help.

Apologies this is perhaps bad form to respond to my own query so shortly after posting it:

This missing step was to convert the report using .toBuffer():

var report = await pdf.generatePdf({html: generated_html});
const buffer = await report.toBuffer();
inspection.report_eng = await Attachment.create({filename: ‘report_eng.pdf’, data:buffer});

Hope it at least helps someone else.

1 Like

Glad we were here to help :stuck_out_tongue: (:duck: ref)

Please note the generated PDF also has a toBase64() method that you can use.

var report = await pdf.generatePdf({html: generated_html});
var pdfBase64 = await report.toBase64();
inspection.report_eng = await Attachment.create({filename: ‘report_eng.pdf’, base64:pdfBase64});
await inspection.save()

Hi Tielman

Thank you for the assist.

As an aside, is it possible to get the url for the saved pdf.

Use case:

  1. Users want to see a preview of the pdf report;
  2. If I use display-file component is gives me a download button only;
  3. My thought is to use the URL as the src for an html component, challenge is I am not sure how to get the url for the saved journey attachment.

Best regards
Matt

Hi Matthew

When I use <display-file /> I do get a preview (see screenshot). This was introduced in RVM v4.80

But if you don’t like this, you can create a fullscreen “preview” using the journey.files.viewFile(attachment); syntax from your App JS. Here is a slightly different use case using the same syntax.

That said, if you want to get the URL of the attachment you can do that from CloudCode or via the Backend API

So you can update your original CC task to also save the URL to the object directly after creating/saving the Attachment, you just need to reload it first. Like so.

var report = await pdf.generatePdf({html: generated_html});
var pdfBase64 = await report.toBase64();
inspection.report_eng = await Attachment.create({filename: ‘report_eng.pdf’, base64:pdfBase64});
await inspection.save()

// reload the object to ensure you get the URLs
await inspection.reload();

if (inspection.report_eng.present()) {
  inspection.report_end_url = inspection.report_eng.url();
  await inspection.save();
}

Please see here for the original post and answer on getting Attachment URLs into the runtime