Generate a PDF from Node

1 Answer 192 Views
PDF Processing
Laurens
Top achievements
Rank 1
Laurens asked on 23 Dec 2021, 08:03 PM

Hi

I'm looking for a way to generate a PDF from React code server side. The library complains about the missing window object, wich is as expected in an browserless environment. Can I pass any options so it won't need window to translate html elements to groups?

import { Handler } from '@netlify/functions'
import { parseHTML } from 'linkedom'
import { drawDOM, exportPDF } from "@progress/kendo-drawing"

export const handler: Handler = async (event, context) => {
  var test = await getBase64()

  return {
    statusCode: 200,
    body: JSON.stringify({
      message: `Error: ${test}!`,
    }),
  }
}

export async function getBase64(): Promise<string> {
  try {
    const { window, document } = parseHTML("<p>test</p>");
    let group = await drawDOM(document.body, { paperSize: "A4" })
    let dataUri = await exportPDF(group)
    return dataUri.split(";base64,")[1]
  } catch (ex) {
    return ex
  }

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 24 Dec 2021, 06:39 AM

Hello,

In general, the PDF export is made to work on the client as it needs valid rendered HTML.

We can customers using tools like puppeteer to simulate a browser on the server, render the HTML, and then use that HTML for the export. This could be a possible way to generate a PDF file only on a NodeJS server.

Regards,
Stefan
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
PDF Processing
Asked by
Laurens
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or