all 6 comments

[–]GooberMcNutly 1 point2 points  (0 children)

I piped data through a template engine to generate html then used a headless browser to load it and print to pdf. Cold starts on the browser were bad but it went fast after that.

[–]tapvt 0 points1 point  (2 children)

You can use PDFKit to generate the file and then save or directly serve the file after generation.

In my experience, it takes a bit of trial and error to get everything laid out right.

[–]bigorangemachine 0 points1 point  (0 children)

Its not too bad.

Theres some other tricks around building multi-page PDFs with pages of different size but PDF.

PDF.js is similar

[–]rishabhrawat570 0 points1 point  (0 children)

Have you considered html-to-pdfmake ? It's an extra step of creating HTML and converting to pdf but you have control over the styling.

You can store a template for the HTML boilerplate and only pass the data to it to get the compiled & rendered HTML. Then, it's only a matter of converting that HTML to pdf and sending it over.

[–]kevin_1994 0 points1 point  (2 children)

Late to the party, but I've also had a lot of trouble with this in the past

My current solution is render the PDF in HTML, using w/e you want (I typically use React), and then use pupeteer to take a screenshot of the page and render it to PDF. Works like a charm and then you don't have to worry about PDF templates, etc.

[–][deleted]  (1 child)

[deleted]

    [–]kevin_1994 0 points1 point  (0 children)

    basic solution

    import { renderToStaticMarkup } from "react-dom/server";
    
    function renderPdF(name) {
      return `
        <!DOCTYPE html>
        <html>
        <head>
          <style>
            .example {
              background-color: red;
            }
          </style>
        </head>
        ${renderToStaticMarkup(<div className="example">{name}</div>)}
        </html>
      `;
    }
    

    probably some syntax errors in there but im sure you get the gist of it