SoFunction
Updated on 2025-04-11

Go implements converting any web page into PDF

In many application scenarios, it may be necessary to convert web page content into PDF format, such as saving web page content, generating reports, or creating website screenshots. This function can be implemented very easily using the Go programming language and combined with some existing libraries. This article will take you step by step on how to convert any web page into a PDF file using Go language.

1. Why choose Go language

Go has the following advantages, making it suitable for handling web pages to PDF tasks:

1. High performance: Go is a compiled language with fast execution speed and is suitable for large-scale processing.

2. Easy to deploy: The binary files generated after Go are built without dependencies, and are easy to deploy and use.

3. Rich library support: Go has multiple excellent third-party libraries that can help us solve different problems.

2. Required tools and libraries

To convert a web page to a PDF, we can use the following tools:

/Chrome or Puppeteer: The browser engine is used to render web pages to ensure that the style and content of the web pages are rendered correctly.

WebDriver or Web automation tool: Used to control Chromium or Chrome to load and render web pages.

-rod: A Go-language web automation tool that allows easy control of the browser and rendering web pages into PDFs.

We will use the `go-rod` library, a lightweight Go library that encapsulates browser automation tools such as Puppeteer and Playwright. `go-rod` supports headless mode Chromium browser and provides a good API to operate the browser, complete web rendering and PDF conversion.

3. Install Go and related dependencies

First, make sure you have the Go environment installed. If Go has not been installed, you can use it from [Go official website](/dl/) Download and install.

Then, we need to install the go-rod` library:

go get /go-rod/rod

go-rod relies on the Chromium browser (or any other browser that supports headless mode). Make sure you have Chromium or Chrome browser installed. If you haven't installed Chromium yet,

It can be installed through the following command:

Install Chromium (taking Ubuntu as an example):

​​​​​​​sudo apt install chromium-browser

Alternatively, you can use other platforms to install it, such as downloading Chromium executables or using Docker to run it.

4. Use Go to implement web page to PDF

In this section, we will use the `go-rod` library to implement a simple program that converts web page content into PDFs.

document:

package main
 
import (
    "fmt"
    "/go-rod/rod"
    "log"
    "os"
)
 
func main() {
    // The URL to convert    url := ""
    // Output PDF file path    outputFile := ""
 
    // Start the headless Chromium browser    browser := ().MustConnect()
 
    // Open the web page    page := (url)
 
    // Set PDF export options    ({
        Path: outputFile, // Output PDF file path    })
 
    ("The web page has been successfully converted to PDF: %s\n", outputFile)
 
    // Close the browser    ()
}

Code parsing:

1. Start the browser:

We use `().MustConnect()` to start a new Chromium instance, which connects to the local Chromium browser.

2. Load the web page:

Use (url) to open the specified webpage URL.

3. Generate PDF:

The () method will render the loaded web page as PDF. We can specify some options through ``, such as the path of the output file, page size, margin, etc.

4. Close the browser:

After the operation is completed, we use () to close the browser instance and release the resource.

Run the program:

After saving the code, execute it in the terminal:

go run 

After successful run, the web page will be converted into a PDF file and saved in the current directory with the file name .

5. Optional function: Customize PDF settings

go-rod provides many custom PDF options, and the following are some commonly used configuration items:

Custom PDF page size

You can set the size of the PDF page, A4 or Letter, etc. through Format.

({
    Path:   "",
    Format: "A4", // Optional: A4, Letter, Legal, etc.})

Custom margins and layouts

You can customize the margins of the PDF page:

({
    Path:  "",
    MarginTop:    0.5,  // Top margin    MarginBottom: 0.5,  // Lower margin    MarginLeft:   0.5,  // Left margin    MarginRight:  0.5,  // Right margin})

Wait for the page to load

If the web page has a lot of dynamic content (for example rendering with JavaScript), you may need to wait for the page to load before converting it to PDF. You can use `()` to ensure that the page is fully loaded:

(url).MustWaitLoad()

Screenshots and other features

In addition to generating PDFs, `go-rod` can also be used to capture screenshots of web pages. You can use the `()` method to intercept the image of the entire web page.

("")

This is the article about Go implementing any web page into PDF. For more related Go web pages to PDF content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!