How to create PDF file. We are looking to generate Invoices for client.
Try out a PDF generator service via Zapier like PDFMonkey.
+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.
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.
Yes, what I did is upload them to my Drive and take the Drive link to update back to the Sheet.
Gsheets > Integromat > Google Docs template
You can skip the Integromat step with single line of App Script
var pdf = DriveApp.getFileById(FILE_ID).getBlob().getAs('application/pdf');
You and your code!
Whoa, thanks!
Hey @Darren_Murphy, can you help me with entire App script for this… in case you have it handy. Thanks in advance!
@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
The following accepts two parameters:
source_doc_id
: ID of the source document/templatefolder_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
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?
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
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();
Cool! Thanks for your Help!