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.
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.
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:
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.
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.
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.
wkgtk-html2pdf supports various distributions, ensuring that users can rely on its functionality across multiple platforms, including:
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.
No comments yet.
Sign in to be the first to comment.