With the development of the Internet, more and more web pages need to be converted into PDF files for printing, sharing and saving. The emergence of Node.js provides us with an environment that can run on the server side, making it more convenient and efficient to convert web pages into PDF files. This article explains how to use Node.js to convert web pages into PDF files.
1. Install Node.js and related modules
To use Node.js to convert web pages into PDF files, you first need to install the Node.js environment on your computer. Node.js can be downloaded and installed from the official website https://nodejs.org/.
After installing Node.js, you need to install some related modules:
Can be installed in the command line tool through the following command:
npm install express puppeteer
2. Write code
After installing Node.js and related modules, start writing code . Below is a simple Express application that automatically converts web pages to PDF files when they are loaded in the browser:
const express = require('express') const puppeteer = require('puppeteer') const app = express() const port = 3000 app.get('/', async (req, res) => { const browser = await puppeteer.launch() const page = await browser.newPage() await page.goto('https://www.example.com') const pdf = await page.pdf() res.contentType("application/pdf") res.send(pdf) await browser.close() }) app.listen(port, () => { console.log(`Server running at http://localhost:${port}`) })
This application uses the Express framework to create a web server and uses the Puppeteer library to convert the web pages into PDF files. Convert to PDF file. Open your browser and enter http://localhost:3000 to see the converted PDF file.
3. Parameter settings
The Puppeteer library can set many parameters to customize during the process of converting web pages into PDF files, such as: page size, margins, header/footer, Zoom and paging etc. Some commonly used parameter settings are listed below:
The following is an example of setting some parameters when converting a web page to a PDF file:
const express = require('express') const puppeteer = require('puppeteer') const app = express() const port = 3000 app.get('/', async (req, res) => { const browser = await puppeteer.launch() const page = await browser.newPage() await page.goto('https://www.example.com') const pdf = await page.pdf({ format: 'A4', margin: { top: '20mm', right: '20mm', bottom: '20mm', left: '20mm' }, displayHeaderFooter: true, headerTemplate: '', footerTemplate: '', scale: 0.8 }) res.contentType("application/pdf") res.send(pdf) await browser.close() }) app.listen(port, () => { console.log(`Server running at http://localhost:${port}`) })
This application sets the A4 page format, 20 mm margins, and display headers and footer, header and footer templates, 0.8x zoom ratio, and control the output PDF file to only include pages 1 to 10.
4. Summary
It is very convenient and efficient to use Node.js to convert web pages into PDF files. The Puppeteer library provides complete control over headless Chrome, allowing you to easily control page size, margins, header/footer, scaling and paging, etc. to meet different needs. Through the introduction of this article, I believe that readers have a certain understanding of the process of converting web pages into PDF files, and can continuously improve and optimize it in practice.
The above is the detailed content of nodejs to pdf. For more information, please follow other related articles on the PHP Chinese website!