feat: cloudflare browser rendering for pdf generation (#562)

* feat: cloudflare browser rendering for pdf generation

* feat: sync env

* fix: imports correction & remove puppeteer

* update return type

* update dependencies

---------

Co-authored-by: Rotimi Best <best@classroomio.com>
This commit is contained in:
Adeyemi Adetunji
2025-06-23 08:55:34 +01:00
committed by GitHub
co-authored by Rotimi Best
parent 179faf0d2c
commit 8f26399741
5 changed files with 3486 additions and 4932 deletions
+27
View File
@@ -0,0 +1,27 @@
import { CLOUDFLARE } from '$src/constants';
export const getCloudflarePdfBuffer = async (html: string, styles?: string) => {
console.log('Generating PDF with Cloudflare API...');
try {
const pdfResponse = await fetch(
`https://api.cloudflare.com/client/v4/accounts/${CLOUDFLARE.CONFIGS.ACCOUNT_ID}/browser-rendering/pdf`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${CLOUDFLARE.CONFIGS.ACCESS_KEY}`
},
body: JSON.stringify({
html: html,
addStyleTag: [{ content: `${styles}` }]
})
}
);
console.log('PDF response status:', pdfResponse.status);
return pdfResponse;
} catch (error) {
console.error('Error generating PDF:', error);
throw new Error(error instanceof Error ? error.message : 'Failed to generate PDF');
}
};