No-code · Updated 2026-12-03
Generate PDFs from Google Sheets
This guide adds a menu to a Google Sheet: select a row, click DevilPDF → Create PDF for selected row, and a finished invoice is saved to a Drive folder with its link written back into the row. It uses Apps Script, which is built into Sheets, and one HTTP call. No add-on to install.
The example is an invoice. Change the template and the columns and the same script makes receipts, quotes or certificates.
#What you need
- A Google account and a spreadsheet you can edit.
- A DevilPDF API key. Sign up, open API keys and create one. It starts with
dp_live_. The free plan includes 500 documents a month.
#The sheet
Row 1 holds headers. Each row below is one invoice with one line item:
| A | B | C | D | E | F | G |
|---|---|---|---|---|---|---|
| Number | Date | Customer | Item | Quantity | Unit price | |
| INV-1001 | 2026-12-03 | Acme Robotics Inc. | Brand identity system | 1 | 4800 |
Column G stays empty. The script fills it with a link to the PDF in Drive.
#1. Store the API key
Open Extensions → Apps Script. In the Apps Script editor, open Project Settings (the gear icon), scroll to Script Properties, and add:
| Property | Value |
|---|---|
DEVILPDF_API_KEY | dp_live_... |
Script properties are stored with the script, not in the code, so the key does not show up when someone reads or copies the script. People who can edit the script project can still open the settings, so use a dedicated key you can revoke.
#2. Add the script
Back in the editor, replace the contents of Code.gs with:
const API_URL = "https://devilpdf.com/v1/render";
const FOLDER_NAME = "Invoices";
function onOpen() {
SpreadsheetApp.getUi()
.createMenu("DevilPDF")
.addItem("Create PDF for selected row", "createPdfForSelectedRow")
.addToUi();
}
function createPdfForSelectedRow() {
const sheet = SpreadsheetApp.getActiveSheet();
const row = sheet.getActiveRange().getRow();
if (row < 2) throw new Error("Select a data row, not the header.");
const url = createPdf(sheet, row);
SpreadsheetApp.getActive().toast("Saved to Drive: " + url, "DevilPDF");
}
function createPdf(sheet, row) {
const [number, date, customer, item, quantity, unitPrice] =
sheet.getRange(row, 1, 1, 6).getValues()[0];
const body = {
template: "invoice",
output: "file",
filename: "invoice-" + number,
data: {
number: String(number),
issue_date: date instanceof Date
? Utilities.formatDate(date, SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "yyyy-MM-dd")
: String(date),
company: { name: "Northwind Studio", address: "410 Bay Street\nToronto, ON M5H 2Y4" },
customer: { name: String(customer) },
items: [{ name: String(item), quantity: Number(quantity), unit_price: Number(unitPrice) }],
tax_rate: 13,
tax_label: "HST",
},
};
const res = UrlFetchApp.fetch(API_URL, {
method: "post",
contentType: "application/json",
headers: { Authorization: "Bearer " + getApiKey() },
payload: JSON.stringify(body),
muteHttpExceptions: true,
});
if (res.getResponseCode() !== 200) {
const error = JSON.parse(res.getContentText()).error;
throw new Error("DevilPDF " + error.type + ": " + error.message);
}
const file = getFolder().createFile(res.getBlob().setName("invoice-" + number + ".pdf"));
sheet.getRange(row, 7).setValue(file.getUrl());
return file.getUrl();
}
function getApiKey() {
const key = PropertiesService.getScriptProperties().getProperty("DEVILPDF_API_KEY");
if (!key) throw new Error("Add DEVILPDF_API_KEY under Project Settings → Script Properties.");
return key;
}
function getFolder() {
const folders = DriveApp.getFoldersByName(FOLDER_NAME);
return folders.hasNext() ? folders.next() : DriveApp.createFolder(FOLDER_NAME);
}Save, then reload the spreadsheet. The DevilPDF menu appears.
#3. Run it
Select any cell in row 2 and click DevilPDF → Create PDF for selected row. The first time, Google asks you to authorize the script: it needs to connect to an external service (DevilPDF) and to create files in your Drive. After that, the PDF lands in an Invoices folder in your Drive and its link appears in column G.
What the script does:
"output": "file"makes DevilPDF return the PDF itself, whichres.getBlob()turns into a file. Nothing is kept on DevilPDF's side.muteHttpExceptions: truelets the script read DevilPDF's error message instead of a generic failure. Errors come back as JSON witherror.typeanderror.message.- A date cell arrives as a
Dateobject.Utilities.formatDateturns it intoYYYY-MM-DDin the spreadsheet's time zone, so the day never shifts. The template prints it in the document's locale. - The
invoicetemplate adds upquantity × unit_priceand the tax. Do not compute totals in the sheet for the PDF's sake.
#Several line items per invoice
Put line items on a second tab, Items, with the columns Invoice number, Item, Quantity, Unit price. Then build the array from every row that matches:
function itemsFor(number) {
const rows = SpreadsheetApp.getActive().getSheetByName("Items").getDataRange().getValues().slice(1);
return rows
.filter((r) => String(r[0]) === String(number))
.map((r) => ({ name: String(r[1]), quantity: Number(r[2]), unit_price: Number(r[3]) }));
}and use items: itemsFor(number) in the body.
#All rows at once
To process every row whose PDF column is empty:
function createMissingPdfs() {
const sheet = SpreadsheetApp.getActiveSheet();
const values = sheet.getDataRange().getValues();
for (let row = 2; row <= values.length; row++) {
if (values[row - 1][0] && !values[row - 1][6]) createPdf(sheet, row);
}
}Add it to the menu with another .addItem("Create all missing PDFs", "createMissingPdfs"). Each row is one request plus one Drive write, so large sheets take a while, and Apps Script limits how long one run can last; for very large sheets, run it in chunks, or send everything to POST /v1/render/batch and collect results with a webhook.
#Other documents and languages
Swap "invoice" for receipt, quote, statement, report, packing-slip or certificate, and send the fields that template expects. Each starter lists them in its sample_data; see templates.
Add a Language column and send locale: String(language) next to template to render in the customer's language. Labels, dates and money switch, and Arabic and Hebrew documents mirror right to left. Fonts for every script are built in.