Buscar contenidos

lunes, 22 de marzo de 2021

Convertir HTML a PDF con wkhtmltopdf



Para los desarrolladores web, es mucho más fácil crear un PDF a partir del formato HTML, básicamente porque proporciona una mejor opción para administrar el PDF. Para esta tarea, los desarrolladores web utilizan bibliotecas como wkhtmltopdf. wkhtmltopdf es una herramienta de línea de comandos para convertir HTML en PDF utilizando el motor de renderizado QT Webkit.

OpenHtmlToPdf

.NET library for rendering HTML documents to PDF format.

OpenHtmlToPdf uses WkHtmlToPdf native Windows library for HTML to PDF rendering.

Download

OpenHtmlToPdf can be download as a [NuGet package] (https://www.nuget.org/packages/OpenHtmlToPdf/)

Usage

Generate PDF with default settings

const string html =

"<!DOCTYPE html>" +

"<html>" +

"<head><meta charset='UTF-8'><title>Title</title></head>" +

"<body>Body text...</body>" +

"</html>";

var pdf = Pdf

.From(html)

.Content();

Defining fluent settings

const string html =

"<!DOCTYPE html>" +

"<html>" +

"<head><meta charset='UTF-8'><title>Title</title></head>" +

"<body>Body text...</body>" +

"</html>";


var pdf = Pdf

.From(html)

.OfSize(PaperSize.A4)

.WithTitle("Title")

.WithoutOutline()

.WithMargins(1.25.Centimeters())

.Portrait()

.Comressed()

.Content();

Defining wkhtmltopdf settings directly

Settings API documentation

const string html =

"<!DOCTYPE html>" +

"<html>" +

"<head><meta charset='UTF-8'><title>Title</title></head>" +

"<body>Body text...</body>" +

"</html>";


var pdf = Pdf

.From(html)

.WithGlobalSetting("orientation", "Landscape")

.WithObjectSetting("web.defaultEncoding", "utf-8")

.Content();


Recursos:

 https://github.com/codaxy/wkhtmltopdf

https://github.com/vilppu/OpenHtmlToPdf

https://parzibyte.me/blog/2019/10/29/convertir-html-pdf-wkhtmltopdf/

https://blog.aspose.com/2020/02/28/convert-html-to-pdf-programmatically-in-csharp-net/


No hay comentarios:

Publicar un comentario