PDF Generator Ne3t.aspx
PDF Generator Ne3t.aspx
Scraping
Web Scraping
Node.js
Puppeteer
Updated on
March 25, 2024
Convert HTML to PDF Using Puppeteer qwäpekg
Converting web pages into PDFs, especially for things like invoices, detailed reports and tables, is super important.
It’s all about making sure the stuff you see in a web page looks just right when turned into a PDF.
Puppeteer is like a magic tool for making this happen. It works with Node.js and empowers developers to
effortlessly navigate web pages and convert HTML into PDFs. In this article, our focus will be on Puppeteer and
Node.js, showcasing how this powerful combination simplifies the process for invoices, reports and tables.
Jump straight to code examples:
Method 1: Generating PDF from a web page URL.
Method 2: Generating PDF from an HTML file.
downloadPdfFromUrl(targetUrl, outputFile)
.then(() => console.log(`PDF downloaded successfully at: ${outputFile}`))
.catch((error) => console.error("Error:", error));
Common use case of this method is invoice URL generation to PDF. Invoices are crucial documents in business
transactions, and converting them to PDFs is a common requirement for archival, sharing, or printing purposes.
The PDF format ensures that the invoice maintains a consistent appearance across different devices and
platforms.
Suppose you have an invoice hosted on a website like Stripe, and you want to generate a PDF from its URL using
Puppeteer. Below is a code example demonstrating this scenario:
const puppeteer = require("puppeteer");
generateInvoicePdfFromUrl(invoiceUrl, outputFile)
.then(() =>
console.log(`Invoice PDF generated successfully at: ${outputFile}`)
)
.catch((error) => console.error("Error:", error));
<!DOCTYPE html>
<html>
<head>
<title>HTML content</title>
</head>
<body>
<h1>Sample</h1>
<div>
<p>
</p><ul>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</li>
<li>Integer interdum felis nec orci mattis, ac dignissim mauris commodo.</li>
</ul>
<p></p>
<p>
</p><ul>
<li>In et augue non turpis faucibus tincidunt a et lectus.</li>
<li>Nulla congue nisi vel diam hendrerit, at pulvinar massa aliquam.</li>
</ul>
<p></p>
</div>
<h1>Ipsum Paragraphs</h1>
<div>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sit amet magna turpis. Donec a tellus in mi pharetra volutpat a
</p>
</div>
</body>
</html>
Here’s a code example that uses Puppeteer to convert this HTML file into a PDF:
const puppeteer = require("puppeteer");
const path = require("path");
downloadPdfFromHtmlFile(inputHtmlFile, outputFile)
.then(() => console.log(`PDF downloaded successfully at: ${outputFile}`))
.catch((error) => console.error("Error:", error));
<html>
<head>
<title>Sample Report</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
color: #333;
}
p{
color: #555;
margin-bottom: 10px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
margin-top: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>Product</th>
<th>Units Sold</th>
<th>Revenue</th>
</tr>
</thead>
<tbody>
<tr>
<td>Product A</td>
<td>150</td>
<td>$5,000</td>
</tr>
<tr>
<td>Product B</td>
<td>120</td>
<td>$4,000</td>
</tr>
<tr>
<td>Product C</td>
<td>200</td>
<td>$6,500</td>
</tr>
</tbody>
</table>
</body>
</html>
`;
await page.setContent(reportHtml);
await browser.close();
await page.pdf({ path: "report.pdf", format: "A4" });
await browser.close();
}
Styling tips
Let's explore styling tips and enhancements for HTML to PDF conversion using Puppeteer.
CSS styling considerations
Styling plays a crucial role in ensuring that the PDF output looks polished and meets specific design requirements.
Here are some considerations:
While Puppeteer supports both inline styles and external stylesheets, inline styles are often more straightforward
for PDF generation.
Consider embedding styles directly within the HTML using the <style> tag for simplicity.
<style>
body {
font-family: 'Arial', sans-serif;
}
h1 {
color: #333;
}
/* ... (additional styles) ... */
</style>
For more advanced styling techniques, consider exploring the following resources:
CSS Tricks: CSS Tricks is a comprehensive resource with articles and guides on various CSS techniques. You
can explore their tips on responsive design, flexbox and grid layout.
MDN Web Docs - CSS: The MDN Web Docs is an excellent reference for CSS properties and values. Dive into their
CSS documentation for in-depth information.
Google Fonts: Google Fonts offers a wide selection of free and open-source fonts. Choose fonts that are not
only pleasing but also supported in PDF rendering.
Conclusion
In this article, we covered two handy methods for turning HTML into PDFs with Puppeteer and Node.js. These
methods are crucial for creating polished documents like invoices, reports and tables. Further, we delved into
styling tips that empower developers to enhance the visual appeal of their PDFs, ensuring a seamless blend of
precision and elegance in their document generation process.
Related Articles
How to Get HTML in Puppeteer?
Anber Arif
TECHNICAL WRITER
Anber Arif, a Software Engineer, leverages over three years of expertise in crafting clear and impactful technical content.
SHARE
PR O D UC T S
F E AT UR E S
USE C A SE S
R E SO UR C E S
C O MPA NY