Home  >  Article  >  Backend Development  >  How to use Golang to convert PDF files

How to use Golang to convert PDF files

PHPz
PHPzOriginal
2023-04-10 14:19:341936browse

Taking the Golang language, which has attracted more and more attention from developers in recent years, as a starting point, this article will explore how to use Golang to realize the function of converting PDF files.

PDF file format is one of the most popular formats for modern electronic documents, so converting PDF files is crucial for certain business scenarios. For Golang, there are many extension libraries available, making it easier to implement PDF conversion functions. Next, let’s take a look at how to use Golang to convert PDF files.

1. Use Go PDF library for conversion

Go PDF library is a popular Golang library that provides many powerful features to help us convert PDF files. We first need to download and install the library. You can use the following command:

go get github.com/signintech/gopdf

After successful installation, we can start PDF conversion. Here is a sample code:

package main

import (
    "github.com/signintech/gopdf"
    "log"
)

func main() {
    pdf := gopdf.GoPdf{}
    pdf.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4})
    pdf.AddPage()
    err := pdf.AddTTFFont("wqy", "/usr/share/fonts/truetype/wqy/wqy-microhei.ttc")
    if err != nil {
        log.Print(err.Error())
        return
    }
    pdf.SetFont("wqy", "", 14)
    pdf.Cell(nil, "你好世界!")
    err = pdf.WritePdf("hello.pdf")
    if err != nil {
        log.Print(err.Error())
        return
    }
}

This code can use the gopdf library to create a new PDF document, then add content to it, and save it to disk. The first three lines of code declare the packages that the program depends on, among which gopdf.Config{PageSize: *gopdf.PageSizeA4} sets the page size to A4. Next, we use the pdf.AddTTFFont() function to add a TrueType font for displaying Chinese characters in PDF files. If the font can be added successfully, use the pdf.Cell(nil, "Hello world!") function to add the string "Hello world!" to the PDF file. Finally, call pdf.WritePdf() to save the PDF file to disk.

2. Use gofpdf library for conversion

gofpdf is another popular Golang PDF library. It also provides many customization features that allow users to better control the conversion results of PDF files. Next, we will use the gofpdf library to convert PDF files.

Similar to the gopdf library, before using the gofpdf library, we need to use the following command to install it:

go get github.com/jung-kurt/gofpdf

After successful installation, let's take a look at the following sample code:

package main

import (
    "github.com/jung-kurt/gofpdf"
)

func main() {
    pdf := gofpdf.New("P", "mm", "A4", "")
    pdf.AddPage()
    pdf.SetFont("Arial", "B", 16)
    pdf.Cell(40, 10, "你好 世界!")
    pdf.OutputFileAndClose("hello.pdf")
}

This sample program creates a new PDF document and adds some content based on it. This code uses the New() function provided by gofpdf to create a new PDF document. The first parameter specifies the page orientation, "P" means portrait, "L" means landscape; the second parameter Specify the unit, here it is millimeters; the third parameter sets the page size, here it is "A4"; the fourth parameter is empty, indicating that no font is specified.

After using the AddPage() function to add a new page, use the SetFont() function to set the font, and use the Cell() function to insert text. Finally, save and close the PDF file using the OutputFileAndClose() function.

Summary

The above introduces two methods of using Golang to convert PDF files. Gopdf and gofpdf are both efficient and stable PDF libraries. They provide very good document support and can Help us convert PDF files easily. Whether you are dealing with complex tasks or simple text conversion tasks, they provide the perfect solution. I believe that as the Golang language continues to develop, these two PDF libraries will become more complete and provide us with better services.

The above is the detailed content of How to use Golang to convert PDF files. 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