DEV Community

Cover image for Stop Using Puppeteer in Serverless: How to Render HTML & Markdown to PDF Without Crashing
Struct.io
Struct.io

Posted on

Stop Using Puppeteer in Serverless: How to Render HTML & Markdown to PDF Without Crashing

A quick, lightweight way to generate styled PDFs from Markdown, HTML, or raw JSON without serverless memory spikes.

If you've ever built an app that handles LLM outputs or developer documentation, you know the struggle of exporting that content cleanly into a PDF.

In this tutorial, we'll walk through a lightweight way to offload your PDF generation pipeline using Loreit Engine via Node.js or fetch.


๐Ÿ› ๏ธ What We're Building

We'll take a string containing mixed Markdown, HTML, and code blocks (like Python or JavaScript snippets) and instantly output a styled, syntax-highlighted PDF stream.

Key Requirements:

  • An API Key (you can grab a free tier key on RapidAPI).
  • Node.js / Fetch runtime environment.

๐Ÿš€ Step 1: Set Up Your Payload

Loreit Engine accepts raw HTML strings, Markdown, or structured JSON payloads. Here is a sample JSON payload containing dynamic content and a code snippet:

{
  "rawText": "<h1>Generated Report</h1><p>Here is the Python function requested:</p><pre><code class=\"language-python\">def calculate_metrics(data):\n    return sum(data) / len(data)</code></pre>"
  "theme": "dark"
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ป Step 2: Send the Request in JavaScript / Node.js

const options = {
  method: 'POST',
  headers: {
    'x-rapidapi-key': 'YOUR_RAPIDAPI_KEY_HERE',
    'x-rapidapi-host': 'loreit-llm-pdf-generator.p.rapidapi.com',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    html: "<h1>Weekly Report</h1><p>Processed via <b>Loreit Engine</b>.</p>"
  })
};

fetch('[https://loreit-llm-pdf-generator.p.rapidapi.com/api/v1/generate-pdf](https://loreit-llm-pdf-generator.p.rapidapi.com/api/v1/generate-pdf)', options)
  .then(response => response.blob())
  .then(blob => {
    console.log("PDF Stream received successfully!");
    // Handle or save the PDF stream in your application
  })
  .catch(err => console.error('Error rendering PDF:', err));
Enter fullscreen mode Exit fullscreen mode

๐Ÿงช Interactive Testing via Postman

If you want to test the endpoint visually without writing code first, you can test it directly in your browser or Postman:

https://documenter.getpostman.com/view/56965602/2sBY4SLymt#intro

Top comments (1)

Collapse
 
struct_io_7b3961a85c4ef48 profile image
Struct.io •

Thanks for reading! Let me know if you hit any issues testing the Postman playground or API endpoint.