PitchHut logo
wkgtk-html2pdf
Convert HTML documents to PDF effortlessly with WebKit2GTK.
Pitch

This lightweight C++ library simplifies the process of generating PDFs from HTML using WebKit2GTK. Ideal for developers looking for a clean, efficient solution, it supports pre-built packages for Arch Linux and Debian, making integration seamless and hassle-free.

Description

wkgtk-html2pdf is a lightweight C++ library designed to facilitate the conversion of HTML documents into PDF files utilizing the advanced rendering capabilities of WebKit2GTK. This library is optimized for performance with minimal dependencies and a small binary footprint, making it an efficient choice for developers looking to integrate HTML-to-PDF capabilities into their applications.

Key Features

  • Tiny Footprint: Maintains a compact size with minimal dependencies, ensuring fast execution and reduced resource utilization.
  • Image Support: Automatically encodes and inlines images in Base64 format, streamlining the PDF generation process.
  • Smart CSS: Comes with a built-in responsive layout system, enhanced by visual margin guides, which helps create well-formatted documents with ease.
  • User-Friendly API: Provides a straightforward C++ API along with comprehensive examples, simplifying implementation.
  • Automated Tag Closing: Allows for HTML generation directly from the API while managing self-closing elements without extra effort.
  • Dynamic Data Integration: Effortlessly integrates C++ data into HTML templates, enabling dynamic document generation.
  • Modern Rendering Engine: Leverages WebKit2GTK to ensure reliable and current rendering outputs.

Why Choose wkgtk-html2pdf?

Many HTML-to-PDF tools face challenges such as improper margin adjustments, inconsistent rendering across devices, and complicated image handling. wkgtk-html2pdf addresses these issues by:

  1. Utilizing CSS variables to gain full control over layout.
  2. Offering visual debugging with border indicators that aid in design adjustments.
  3. Automatically managing common PDF rendering intricacies to enhance user experience.

Quick Start

Command Line Interface

For users seeking a quick and straightforward way to produce PDFs from HTML files without any coding, the command line interface provides an intuitive solution:

html2pdf -i infile.html -o outfile.pdf -O portrait -s A4

To optimize results, it is advisable to link to the appropriate page template, ensuring that content is organized within page and subpage elements to avoid margin overflow.

C++ API Example

With the C++ API, users can programmatically create PDFs as follows:

#include <wk2gtkpdf/ichtmltopdf++.h>
#include <wk2gtkpdf/pretty_html.h>

int main() {
    icGTK::init();
    WEBPAGE html("<!DOCTYPE html>");
    html_tree dom("html", html);
     
    html_tree *head = dom->new_node("head");
    head->new_node("link", "rel=\"stylesheet\" href=\"/usr/share/wk2gtkpdf/A4-portrait.css\"");
    
    html_tree *body = dom.new_node("body");
    html_tree *page = body->new_node("div", "class=\"page\"");
    html_tree *subpage = page->new_node("div", "class=\"subpage\"");

    subpage->new_node("h1")->set_node_content("Hello World");
    
    pretty_html::process_nodes(&dom);
    PDFprinter pdf;
    pdf.set_param(html, "/tmp/output.pdf");
    pdf.layout("A4", "portrait");
    pdf.make_pdf();
    
    return 0;
}

The generated PDF file will be located in the current working directory; absolute paths can be utilized for designated output locations.

Advanced Features

Choosing wkgtk-html2pdf also means access to guided templates for setting up standardized page sizes and margin configurations swiftly:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="/usr/share/icprint/A5-portrait.css">
    </head>
    <body>
        <div class="page">
            <div class="subpage">
                Hello World
            </div>
        </div>
    </body>
</html>

Furthermore, output options include file writing to specific paths or generating binary large objects (BLOBs) suitable for integration with APIs such as ImageMagick.

Compatibility

wkgtk-html2pdf supports various distributions, ensuring that users can rely on its functionality across multiple platforms, including:

  • Arch Linux: Fully supported (AUR + direct download)
  • Debian Trixie: Fully supported (direct download)
  • Debian Bookworm: Supported (direct download)
  • Ubuntu 22.04+: Supported (direct download)

For additional details, issues, or feature requests, contributions can be made directly through the GitHub repository. This project not only simplifies the conversion of HTML to PDF but also enhances efficiency and output quality for developers across various domains.

0 comments

No comments yet.

Sign in to be the first to comment.