nodejs to pdf

WBOY
WBOYOriginal
2023-05-23 12:27:071772browse

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:

  1. Express: Provides a web application development framework.
  2. Puppeteer: A Node.js library developed by Google for controlling headless Chrome.

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:

  1. format: The page format of the web page. It can be set to standard page formats such as A4, Letter, etc., or it can be a customized page size.
  2. margin: The margin of the web page. Can be set to top margin, bottom margin, left margin, right margin, etc.
  3. headerTemplate/footerTemplate: HTML template that can set header and footer. You can add an element with a class name of page in the template and add the converted page number in it.
  4. displayHeaderFooter: You can set whether to display the header and footer. Set to true to display, set to false to not display.
  5. scale: You can set the scaling ratio of the web page to adapt to the output PDF file.
  6. pageRanges: You can set the page number range that needs to be converted to adapt to the conversion of multi-page documents. For example, pageRanges: '1-10' means that only the content from pages 1 to 10 will be converted.

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: '<div class="page-number"></div>',
    footerTemplate:
      '<div class="page-number"></div>',
    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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:jquery menu plugin?Next article:jquery menu plugin?