Public
Back
Version 6
2/6/2024
import { email as sendEmail } from "https://esm.town/v/std/email?v=9";
import puppeteer from "npm:puppeteer-core";
/**
* This requires a free API token from browserless.io saved in your
* Env Variables as BROWSERLESS_API_KEY
*/
let sendPDF = async (subject, html) => {
const browserlessApi = Deno.env.get("BROWSERLESS_API_KEY");
const browser = await puppeteer.connect({
browserWSEndpoint: `wss://chrome.browserless.io?token=${browserlessApi}`,
});
const page = await browser.newPage();
await page.setContent(html);
const screenshot = await page.pdf();
await page.close();
await browser.close();
return sendEmail({
subject: "PDFified: " + subject,
// html: html,
attachments: [{
content: screenshot.toString("base64"),
filename: (subject || "Untitled") + ".pdf",
type: "application/pdf",
disposition: "attachment",
}],
});
};
export default async function(email: Email) {
console.log(email);
await sendPDF(email.subject, email.html);
}
Updated: February 6, 2024