New: PDF/A-3b conversion with a compliance report included.See Docs

How to Add Image to PDF using Google Apps Script and PDF.co

Add a logo, stamp, or other image to an existing PDF directly from Google Sheets. This example reads the PDF and image URLs from your spreadsheet, sends them to PDF.co, and writes the generated PDF’s download URL back to the sheet.

Prepare Your Spreadsheet

Create a Google spreadsheet and enter the following values on the sheet you will use:

  • B1: Your PDF.co API key, available from your PDF.co account.
  • A4: A direct URL to the source PDF.
  • B4: A direct URL to the image you want to insert.
  • C4: Leave this cell empty. The script will write the output URL here.

Both source URLs must be accessible to PDF.co. Keep the spreadsheet private while B1 contains your API key.

Add the Apps Script

In Google Sheets, open Extensions → Apps Script. Paste the following code into the spreadsheet’s bound script project and save it.

function onOpen() {
  SpreadsheetApp.getUi()
    .createMenu("PDF.co")
    .addItem("Add Image to PDF", "addImageToPDF")
    .addToUi();
}

function addImageToPDF() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

  const apiKey = sheet.getRange("B1").getDisplayValue().trim();
  const pdfUrl = sheet.getRange("A4").getDisplayValue().trim();
  const imageUrl = sheet.getRange("B4").getDisplayValue().trim();
  const resultUrlCell = sheet.getRange("C4");

  if (!apiKey || !pdfUrl || !imageUrl) {
    resultUrlCell.setValue(
      "Enter your API key in B1, PDF URL in A4, and image URL in B4."
    );
    return;
  }

  const data = {
    "async": false,
    "encrypt": false,
    "inline": false,
    "name": "sample_result.pdf",
    "url": pdfUrl,
    "imagesString": `260;350;0-;${imageUrl}`
  };

  const options = {
    "method": "post",
    "contentType": "application/json",
    "headers": {
      "x-api-key": apiKey
    },
    "payload": JSON.stringify(data),
    "muteHttpExceptions": true
  };

  try {
    const response = UrlFetchApp.fetch(
      "https://api.pdf.co/v1/pdf/edit/add",
      options
    );

    const pdfCoRespJson = JSON.parse(response.getContentText());

    if (pdfCoRespJson.error) {
      resultUrlCell.setValue(
        pdfCoRespJson.message || "PDF.co returned an error."
      );
    } else if (pdfCoRespJson.url) {
      resultUrlCell.setValue(pdfCoRespJson.url);
    } else {
      resultUrlCell.setValue(
        "No output URL returned. Check the response and request settings."
      );
    }
  } catch (error) {
    resultUrlCell.setValue(
      "The request could not be completed. Check the script execution details."
    );
    throw error;
  }
}

Set the Image Position and Pages

The imagesString value controls the image placement:

"imagesString": `260;350;0-;${imageUrl}`

In this example:

  • 260: The horizontal position, measured from the left edge.
  • 350: The vertical position, measured from the top edge.
  • 0-: Adds the image to every page, starting with the first.
  • imageUrl: Uses the image URL entered in B4.

Coordinates use PDF points, with 72 points equal to one inch. Use the PDF Inspector to determine suitable coordinates for your document.

To insert the image only on the first page, change 0- to 0:

"imagesString": `260;350;0;${imageUrl}`

The request uses inline: false because the script expects a download URL to write into C4. See the PDF Add documentation for additional image-placement options.

Run the Script and Save the PDF

Reload the spreadsheet to display the custom menu. Make sure the sheet containing your input values is active, then select PDF.co → Add Image to PDF.

Follow the authorization prompts on your first run. After the request completes, open the output URL in C4.

Check that the image appears in the expected position and on the correct pages. Download and save the PDF if you need it after the temporary link expires.

Conclusion

You can now insert an image into a PDF from Google Sheets using Apps Script and PDF.co. Update the input URLs, coordinates, or page selection to reuse the script for other documents.

Related Tutorials

See Related Tutorials