How to Add Chapters to PDF: A Step-by-Step Guide with UniHTML
Creating a well-organized PDF document with chapters can be essential for structuring content in a professional and accessible format. If you want to generate PDFs programmatically from HTML content while keeping the formatting and assets intact, UniHTML offers an efficient solution.
In this guide, we will walk you through the process of adding chapters to a PDF using the UniHTML library, demonstrating how to convert multi-file HTML content into a neatly structured PDF document. Let’s dive into the details step by step.
Introduction
This guide will demonstrate how to create a PDF document with chapters from multi-file HTML content using the UniHTML library. The HTML resources, such as CSS and images, are organized in a single directory and loaded from there, then rendered and converted to PDF. By using this method, you can create professional PDF documents that maintain the original styling and formatting of your HTML content.
UniHTML, a versatile tool by UniDoc, simplifies the process of generating PDF documents from web content. The library is especially useful for developers looking to automate PDF creation and customize document structures programmatically. This step-by-step guide will show you how to add chapters to a PDF using UniHTML.
Prerequisites:
Before proceeding, make sure you have:
- A UniCloud account to obtain your API key.
- Basic knowledge of Go programming language.
- A UniHTML server set up and ready to use.
- Git installed for cloning the project repository.
If you are new to UniPDF SDK, follow the getting started guide to set up your local development environment.
Step 1: Setting Up Your Environment
First, you need to get an API key from your UniCloud account. If you don’t have one, sign up on the UniCloud website and generate a free API key.
Next, ensure you have set up a local development environment for UniPDF SDK. Follow the UniHTML setup guide to install the necessary components. Make sure the UniHTML server is up and running before proceeding with the steps below.
Step 2: Cloning the Project Repository
The next step is to clone the example project repository, which contains the Go code needed for this guide. Open a terminal and run the following command to clone the repository:
git clone [email protected]:unidoc/unihtml-examples.git
Navigate to the directory
folder within the unihtml-examples
directory:
cd unihtml-examples/directory
This directory contains the sample code required to create a PDF document with chapters from multi-file HTML content.
Step 3: Understanding the Code
Let’s break down the code and understand how it works. The provided Go code demonstrates the process of creating a PDF document from HTML content in a specified directory, using the UniHTML library.
Importing Libraries
The necessary libraries are imported in the initial lines:
import (
"fmt"
"os"
"github.com/unidoc/unihtml"
"github.com/unidoc/unipdf/v3/common/license"
"github.com/unidoc/unipdf/v3/creator"
)
These libraries provide functions for handling the HTML-to-PDF conversion, setting up the licensing, and managing the creation process.
Loading the API Key
The init
function is responsible for loading the metered License API key from the system environment:
func init() {
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
if err != nil {
panic(err)
}
}
Here, the API key is retrieved from an environment variable named UNIDOC_LICENSE_API_KEY
and set using the SetMeteredKey
function. This step ensures that the library is properly licensed for use.
Main Function Breakdown
The main
function is where the action happens. It contains the code for connecting to the UniHTML server, creating a PDF document, and writing chapters based on HTML content.
Checking Command-Line Arguments
The code checks for the correct number of command-line arguments, which should include the path to the UniHTML server:
if len(os.Args) != 2 {
fmt.Println("Err: provided invalid arguments. No UniHTML server path provided")
os.Exit(1)
}
If the server URL is not provided, the program exits with an error message.
Connecting to the UniHTML Server
The connection to the UniHTML server is established using the Connect function:
if err := unihtml.Connect(os.Args[1]); err != nil {
fmt.Printf("Err: Connect failed: %v\n", err)
os.Exit(1)
}
This step is crucial for rendering the HTML content and converting it to PDF.
Initializing the PDF Creator
A new Creator
object is created, which will handle the PDF generation:
c := creator.New()
The Creator
object is essential for managing the overall PDF creation process.
Creating a New Chapter
The code creates a new chapter within the PDF:
ch := c.NewChapter("Directory")
Here, a chapter named “Directory” is added to the PDF. You can customize the chapter’s name as needed.
Loading HTML Content from the Directory
The HTML content, along with associated CSS and images, is loaded from a specified directory:
htmlDocument, err := unihtml.NewDocument("data")
if err != nil {
fmt.Printf("Err: NewDocument failed: %v\n", err)
os.Exit(1)
}
The NewDocument
function reads all the files from the “data” directory, ensuring that CSS styles and images are correctly applied in the PDF.
Adding the Document to the Chapter
The loaded HTML content is added to the chapter:
if err = ch.Add(htmlDocument); err != nil {
fmt.Printf("Err: Adding HTML Document failed: %v\n", err)
os.Exit(1)
}
This step integrates the formatted content into the chapter, allowing it to be rendered in the final PDF document.
Drawing the Chapter
The chapter is then rendered using the Draw
method:
if err = c.Draw(ch); err != nil {
fmt.Printf("Err: Draw failed: %v\n", err)
os.Exit(1)
}
This method ensures the chapter is properly formatted and included in the PDF.
Writing the Output to a File
Finally, the generated PDF is written to a file named “directory_chapter.pdf”:
if err = c.WriteToFile("directory_chapter.pdf"); err != nil {
fmt.Printf("Err: %v\n", err)
os.Exit(1)
}
At this point, your PDF document with the chapter created from the HTML content is ready.
Step 4: Running the Code
To run the code and generate the PDF file, use the following command in your terminal:
go run directory_chapter.go <server address>
Replace <server address>
with the actual address of your UniHTML server, for example, localhost:8080
if it is running locally on port 8080.
The program will connect to the server, process the HTML content in the “data” directory, and produce a PDF file named “directory_chapter.pdf” in the current directory. Open the PDF file to review the content, which should be structured with chapters based on your HTML files.
Conclusion:
By following the steps outlined in this guide, you can effectively add chapters to a PDF using the UniHTML library. This approach allows for converting HTML content—along with associated assets like CSS and images—into a neatly formatted PDF document. With UniHTML, creating professional documents that retain the original styling of your web content becomes straightforward.