html to pdf nodejs

// Step 1: Install the required npm packages
const express = require('express');
const puppeteer = require('puppeteer');

// Step 2: Set up an Express app
const app = express();
const port = 3000;

// Step 3: Create a route to handle HTML to PDF conversion
app.get('/html-to-pdf', async (req, res) => {
  // Step 4: Launch a headless browser using Puppeteer
  const browser = await puppeteer.launch();

  // Step 5: Open a new page in the browser
  const page = await browser.newPage();

  // Step 6: Set the HTML content to be converted to PDF
  const htmlContent = '<html><body><h1>Hello, PDF!</h1></body></html>';
  await page.setContent(htmlContent);

  // Step 7: Generate a PDF from the HTML content
  const pdfBuffer = await page.pdf();

  // Step 8: Close the browser
  await browser.close();

  // Step 9: Set the response headers for PDF content
  res.setHeader('Content-Type', 'application/pdf');
  res.setHeader('Content-Disposition', 'inline; filename=example.pdf');

  // Step 10: Send the generated PDF as the response
  res.send(pdfBuffer);
});

// Step 11: Start the Express server
app.listen(port, () => {
  console.log(`Server is running at http://localhost:${port}`);
});