Share on Social Media

Discover the precise steps to effortlessly learn how to install WKHTMLTOPDF on Rocky Linux 9. Follow our comprehensive guide to seamlessly integrate this powerful tool into your workflow, unlocking the ability to convert web pages into PDF documents with ease. #centlinux #linux

What is WKHTMLTOPDF?

WKHTMLTOPDF is an open-source command-line tool that converts HTML pages to PDF documents using the WebKit rendering engine. It allows users to generate high-quality PDF files from web content, including dynamic HTML pages, by rendering them as they would appear in a web browser and then converting them into a PDF format. WKHTMLTOPDF supports various features such as headers, footers, page numbering, table of contents, and more, making it a versatile tool for generating PDF documents from web content. It’s commonly used in web development, reporting, and document generation tasks.

What is WKHTMLTOX?

WKHTMLTOX is a combination of wkhtmltopdf and wkhtmltoimage, that are open source (LGPLv3) command line tools to render HTML into PDF and various image formats using the Qt WebKit rendering engine. These run entirely “headless” and do not require a display or display service.

WKHTMLTOX Alternatives?

“WKHTMLTOX” is a term that encompasses both “WKHTMLTOPDF” and “WKHTMLTOIMAGE.” These are open-source command-line tools that convert HTML pages to PDF documents and images, respectively, using the WebKit rendering engine.

Alternatives to WKHTMLTOX include:

  1. Headless Chrome or Chromium: These browsers can be run in headless mode to render HTML pages and capture screenshots or generate PDFs using built-in functionality. Tools like Puppeteer or Selenium can automate this process.
  2. PrinceXML: PrinceXML is a commercial tool known for high-quality PDF generation from HTML and CSS. It supports advanced features like CSS3, JavaScript, and XML-based formats.
  3. wkhtmltopdf (and wkhtmltoimage): These are the predecessors of WKHTMLTOX and are still used in some cases. They provide similar functionality for converting HTML to PDF and images.
  4. PhantomJS: Although PhantomJS is no longer actively maintained, it was popular for headless website testing and could be used to capture screenshots or render HTML to PDF.
  5. Puppeteer: Puppeteer is a Node.js library that provides a high-level API for controlling headless Chrome or Chromium. It can be used to capture screenshots or generate PDFs from web pages.
  6. WeasyPrint: WeasyPrint is a visual rendering engine for HTML and CSS that outputs PDF files. It’s based on various web standards, including HTML5, CSS3, and JavaScript.

These alternatives offer varying features, performance, and ease of use, so you can choose the one that best fits your requirements and workflow.

Recommended Online Training: Becoming a Linux Power User

5537320 b4f9show?id=oLRJ54lcVEg&offerid=1074652.5537320&bids=1074652

Environment Specification

We are using a minimal Rocky Linux 9 operating system with following specifications.

  • CPU – 3.4 Ghz (2 cores)
  • Memory – 2 GB
  • Storage – 20 GB
  • Operating System – Rocky Linux release 9.1 (Blue Onyx)
  • Hostname – rocky9.centlinux.com
  • IP Address – 192.168.88.128/24

Prepare your Rocky Linux Server

Login as root user on your Rocky Linux server, with the help of a ssh client.

Update software packages in your Linux operating system by using dnf package manager.

# dnf update -y

Occasionally, above command also updates your Linux Kernel. If it happens, then you should reboot your Linux operating system before moving forward in this Linux tutorial.

# reboot

After reboot, check the Linux operating system and Kernel versions.

# cat /etc/os-release
NAME="Rocky Linux"
VERSION="9.1 (Blue Onyx)"
ID="rocky"
ID_LIKE="rhel centos fedora"
VERSION_ID="9.1"
PLATFORM_ID="platform:el9"
PRETTY_NAME="Rocky Linux 9.1 (Blue Onyx)"
ANSI_COLOR="0;32"
LOGO="fedora-logo-icon"
CPE_NAME="cpe:/o:rocky:rocky:9::baseos"
HOME_URL="https://rockylinux.org/"
BUG_REPORT_URL="https://bugs.rockylinux.org/"
ROCKY_SUPPORT_PRODUCT="Rocky-Linux-9"
ROCKY_SUPPORT_PRODUCT_VERSION="9.1"
REDHAT_SUPPORT_PRODUCT="Rocky Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="9.1"

# uname -r
5.14.0-162.18.1.el9_1.x86_64

WKHTMLTOX is not available in standard yum repositories. But you can download the latest version of WKHTMLTOX from their official website.

To download WKHTMLTOX rpm you may need wget command. Therefore, install it now with the help of dnf command.

# dnf install -y wget

Install WKHTMLTOX on Linux

Now, use wget command to download WKHTMLTOX rpm. You can either use the following commands to download WKHTMLTOX or get the URL of latest version from their website.

# cd /tmp
# wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6.1-2/wkhtmltox-0.12.6.1-2.almalinux9.x86_64.rpm
...
Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.109.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 16198412 (15M) [application/octet-stream]
Saving to: ‘wkhtmltox-0.12.6.1-2.almalinux9.x86_64.rpm’

wkhtmltox-0.12.6.1- 100%[===================>]  15.45M   515KB/s    in 20s

2023-03-27 12:08:50 (801 KB/s) - ‘wkhtmltox-0.12.6.1-2.almalinux9.x86_64.rpm’ saved [16198412/16198412]

Execute following command to install WKHTMLTOX on your Rocky Linux server.

# dnf localinstall -y wkhtmltox-0.12.6.1-2.almalinux9.x86_64.rpm

After installation, check the versions of wkhtmltopdf and wkhtmltoimage commands.

# wkhtmltopdf --version
wkhtmltopdf 0.12.6.1 (with patched qt)

# wkhtmltoimage --version
wkhtmltoimage 0.12.6.1 (with patched qt)

WKHTMLTOX has been installed successfully.

You can use –help switch as follows to get help on wkhtmltopdf and wkhtmltoimage commands.

# wkhtmltopdf --help
# wkhtmltoimage --help

WKHTMLTOX Examples

Basic WKHTMLTOPDF Example

Here’s a simple example of how you can use wkhtmltopdf to convert an HTML file to a PDF:

Let’s say you have an HTML file named example.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Example HTML to PDF</title>
</head>
<body>
    <h1>Hello, World!</h1>
    <p>This is a simple example HTML file.</p>
</body>
</html>

To convert this HTML file to a PDF named output.pdf, you can use the wkhtmltopdf command like this:

wkhtmltopdf example.html output.pdf

This command will generate a PDF file named output.pdf based on the content of example.html.

You can also specify options to customize the PDF output, such as setting page size, margins, header, and footer. For example:

wkhtmltopdf --page-size A4 --margin-top 10mm --margin-bottom 10mm --margin-left 10mm --margin-right 10mm example.html output.pdf

This command will generate a PDF with A4 page size and 10mm margins on all sides.

Feel free to adjust the options according to your requirements!

Basic WKHTMLTOIMAGE Example

Certainly! Here’s an example of how you can use wkhtmltoimage to convert an HTML file to an image:

Let’s say you have an HTML file named example.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Example HTML to Image</title>
</head>
<body>
    <h1>Hello, World!</h1>
    <p>This is a simple example HTML file.</p>
</body>
</html>

To convert this HTML file to an image, you can use the wkhtmltoimage command like this:

wkhtmltoimage example.html output.png

This command will generate a PNG image file named output.png based on the content of example.html.

You can also specify options to customize the output, such as setting width and height, quality, and format. For example:

wkhtmltoimage --quality 90 --width 800 --format jpg example.html output.jpg

This command will generate a JPEG image file with a quality of 90, a width of 800 pixels, based on the content of example.html.

Feel free to adjust the options according to your requirements!

Use WKHTMLTOPDF in PHP

Sure! Here’s a simple PHP example demonstrating how you can use wkhtmltopdf library in PHP to convert HTML content to a PDF file:

<?php

// Define the HTML content to be converted to PDF
$htmlContent = '
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Example HTML to PDF</title>
</head>
<body>
    <h1>Hello, World!</h1>
    <p>This is a simple example HTML file.</p>
</body>
</html>';

// Set the path to the wkhtmltopdf executable
$wkhtmltopdfPath = '/usr/local/bin/wkhtmltopdf'; // Adjust this path according to your installation

// Define the input and output file paths
$inputFilePath = 'input.html';
$outputFilePath = 'output.pdf';

// Write the HTML content to a file
file_put_contents($inputFilePath, $htmlContent);

// Execute the wkhtmltopdf command to convert HTML to PDF
exec("$wkhtmltopdfPath $inputFilePath $outputFilePath");

// Check if the output PDF file was created successfully
if (file_exists($outputFilePath)) {
    echo 'PDF file generated successfully!';
} else {
    echo 'Error generating PDF file.';
}

// Remove the input HTML file after conversion
unlink($inputFilePath);

?>

In this example:

  • We define the HTML content that we want to convert to a PDF.
  • We set the path to the wkhtmltopdf executable. Make sure to adjust this path according to the location of the wkhtmltopdf binary on your system.
  • We define the input HTML file path and the output PDF file path.
  • We write the HTML content to a file named input.html.
  • We execute the wkhtmltopdf command using exec() to convert the HTML file to a PDF file.
  • We check if the output PDF file was created successfully and provide appropriate feedback.
  • We remove the input HTML file after the conversion process is completed.

You’ll need to adjust the paths and configurations according to your environment and requirements.

Use WKHTMLTOPDF in Python

Certainly! Below is a basic example of how you can use the pdfkit library in Python, which is a Python wrapper for wkhtmltopdf, to convert HTML content to a PDF file:

First, make sure you have installed the pdfkit library. You can install it via pip:

# pip install pdfkit

Then, you’ll need to have wkhtmltopdf installed on your system. Ensure it’s properly installed and accessible in your system’s PATH.

Here’s the Python code:

import pdfkit

# HTML content to be converted to PDF
html_content = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Example HTML to PDF</title>
</head>
<body>
    <h1>Hello, World!</h1>
    <p>This is a simple example HTML file.</p>
</body>
</html>
"""

# Save HTML content to a temporary file
with open('temp.html', 'w') as f:
    f.write(html_content)

# Convert HTML to PDF
pdfkit.from_file('temp.html', 'output.pdf')

print("PDF file generated successfully!")

# Cleanup: Remove the temporary HTML file
import os
os.remove('temp.html')

In this example:

  • We define the HTML content that we want to convert to a PDF.
  • We save the HTML content to a temporary HTML file named temp.html.
  • We use pdfkit.from_file() function to convert the HTML file to a PDF file named output.pdf.
  • Finally, we print a message indicating that the PDF file has been generated successfully and remove the temporary HTML file.

Make sure to adjust paths and configurations according to your specific requirements.

Video: How to install WKHTMLTOPDF on Linux

YouTube player

Final Thoughts

Master the installation process of WKHTMLTOPDF on Rocky Linux 9 with our comprehensive guide. Enhance your document generation capabilities by seamlessly integrating this powerful tool into your workflow. Empower yourself to effortlessly convert HTML pages into high-quality PDF documents, unlocking new possibilities for your projects.

Leave a Reply

Your email address will not be published. Required fields are marked *