Creating PDF

How to create PDF file. We are looking to generate Invoices for client.

1 Like

Try out a PDF generator service via Zapier like PDFMonkey.

7 Likes

+1 for PDFMonkey, but make sure you have an understanding of HTML and JSON to make a nice template. The rest is just about matching the right columns to the JSON generated from Sheets.

6 Likes

The goal then is to save the download link of the generated PDF in the sheet. And this is possible with an additional task.
I’ve tried and it works fine.

2 Likes

Yes, what I did is upload them to my Drive and take the Drive link to update back to the Sheet.

1 Like

Gsheets > Integromat > Google Docs template

You can skip the Integromat step with single line of App Script :wink:

var pdf = DriveApp.getFileById(FILE_ID).getBlob().getAs('application/pdf');

10 Likes

You and your code! :stuck_out_tongue:

1 Like

Whoa, thanks!

Hey @Darren_Murphy, can you help me with entire App script for this… in case you have it handy. Thanks in advance! :slight_smile:

2 Likes

:crazy_face:

1 Like

@ThinhDinh How did you solve the problem of replacing, in the preparation of the JSON data packet, the special characters such as ’ e " (quote and double quote) with the relative escape sequences? Can this be done simply using only the data editor in your opinion?

With “simply” I mean without using split & join techinique…

I didn’t have one handy, but I tossed a a generic example together :stuck_out_tongue:

The following accepts two parameters:

  1. source_doc_id: ID of the source document/template
  2. folder_id: ID of the folder where the PDF will be saved

It does the following:

  • Makes a temporary copy of the source document
  • Adds a “random” line of text to that copy
  • Creates a PDF copy
  • Moves that PDF to the target folder
  • Trashes the temporary file
Summary
function convert_doc_to_pdf(source_doc_id, folder_id) {
   
  // Make a copy of the original document
  var doc_name = DriveApp.getFileById(source_doc_id).getName();
  var temp_file = DriveApp.getFileById(source_doc_id).makeCopy('temp');
  var temp_doc_id = temp_file.getId();
  
  // Open the copy, add some random text, save and close it
  var temp_doc = DocumentApp.openById(temp_doc_id);
  var body = temp_doc.getBody();
  body.appendListItem('Some random text');
  temp_doc.saveAndClose();
  
  // Create the PDF and move it to the target folder
  var blob = DriveApp.getFileById(temp_doc_id).getBlob().getAs('application/pdf');
  blob.setName(doc_name + '.pdf');
  var pdf = DriveApp.createFile(blob);
  var folder = DriveApp.getFolderById(folder_id);
  pdf.moveTo(folder);
  
  // Trash the temp copy of the original
  temp_file.setTrashed(true);
}

Any questions, just ask :smiley:

6 Likes

Great! Thank you so much for your help and time!
Just 1 thing, how to to get the file id back in the Google sheets?

The File ID is shown in the URL when you open a doc:

That’s tricky. Admittedly I haven’t faced that (I think) but can you try these escape characters.

%22 for double quote
%27 for single quote

1 Like

Sorry, I was not clear with my Question.
What I wanted to ask is if we can get the file id of the Pdf generated in the Google sheet. So as to display the same directly in the app.

oh, right…
Referring back to that example script, you can just use pdf.getId();

1 Like

Cool! Thanks for your Help! :slight_smile: