Tuesday, November 19, 2013

How to send PDF reports with AdWords scripts

AdWords scripts allow you to create and email PDF documents, complete with pretty formatting and images. You can schedule a script that emails a professional-looking PDF report to your client weekly, complete with logos, watermarks, signatures, and other artwork. This blog post discusses this feature in more details.

Sending an attachment

The following snippet sends an email with an attachment:
// assuming you have a good-to-go blob, more on that later.
MailApp.sendEmail(
    "us...@example.com",
    "TPS report",
    "Please find the TPS report attached to this email",
    {attachments: [blob]}
);
BLOB stands for Binary Large OBject. In Google Apps Script, blobs are used as the in-between data format that allows different services to pass data to each other.

Creating a BLOB

Start with an HTML string, and use Utilities.newBlob() to convert it into a blob:
var html = "<h1>TPS report</h1><p>Please read this carefully and respond " +
    "by end of day...</p>"
var blob = Utilities.newBlob(html, MimeType.HTML);
Converting a blob into PDF is as easy as
var pdfBlob = blob.getAs(MimeType.PDF);
Dealing with images

A reasonable attempt looks like this:
var html = "<img src='url_of_the_image'>";
But that, unfortunately, won't work. The PDF generator never actually fetches resources that your HTML links to, so the image won't make it into the PDF. You need to specify the source of the image inline instead, as a base64-encoded string:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." />
Don't worry about the contents of the base64-encoded strings - they are gibberish and will consume lots of screen space if printed out.

Putting it all together

The following snippet will fetch the image from imageUrl, embed it into the PDF, and email the results to us...@example.com:
// fetch the image. This example assumes it's a PNG.
var imageBlob = UrlFetchApp.fetch(imageUrl).getBlob();

// grab its bytes and base64-encode them.
var base64EncodedBytes = Utilities.base64Encode(imageBlob.getBytes());

// create HTML.
var html = "<h1>TPS report</h1><img src='data:image/png;base64," +
    base64EncodedBytes + "'/>";

// create a blob, convert it to PDF.
var htmlBlob = Utilities.newBlob(html, MimeType.HTML);
var pdfBlob = htmlBlob.getAs(MimeType.PDF);

// give it a name - this will become the file name of the attachment.
pdfBlob.setName("tps_report.pdf");

// send the email.
MailApp.sendEmail(
    "us...@example.com",
    "TPS report",
    "Please find the TPS report attached to this email",
    {attachments: [pdfBlob]}
);
Instead of fetching the image from the URL, you can upload it to Google Drive and use DriveApp to fetch it:
var blobFromDrive = DriveApp.getFilesByName(imageFileName).next().getBlob();
So there you have it - blobs aren't just fun to say, they're useful as well. If you have questions or feedback about this feature or AdWords scripts in general, you can post them on our developer forum or our Google+ page.