Skip to content

DevEnterpriseSoftware/Html2Pdf

Repository files navigation

HTML-2-PDF API Service

Provides endpoints to convert raw HTML into a PDF document with configurable options.

The container starts up a headless Chrome browser instance (JavaScript and service workers disabled) and renders the provided HTML to export a PDF. Host this service privately and only pass trusted HTML content.

Endpoints

Method Path Description
GET / Returns the current API version
POST / Converts HTML to a PDF file

POST Request Body

Field Type Required Default Description
html string Yes Raw HTML content to convert
fileName string No "file" Output filename (without extension)
format string No "Letter" Page size: Letter, Legal, Tabloid, Ledger, A0A6
printBackground bool No true Whether to include CSS backgrounds

Usage

The container exposes port 5000 so you can bind that to a port number of your choice (8080 in the example below).

docker run -p 8080:5000 deventerprisesoftware/html2pdf

Examples

cURL

curl \
  -d '{ "html": "<h1>HTML 2 PDF</h1><p>Sample text</p>", "format": "A4" }' \
  -H "Content-Type: application/json" \
  --output test_file.pdf \
  -X POST "http://localhost:8080/"

C#

using System.Net.Http;
using System.Text;
using System.Text.Json;

var request = JsonSerializer.Serialize(new
{
    Html = "<h1>HTML 2 PDF</h1><p>Sample text</p>",
    FileName = "test_file",
    Format = "A4"
});

using var httpClient = new HttpClient();
using var requestContent = new StringContent(request, Encoding.UTF8, "application/json");

var response = await httpClient.PostAsync("http://localhost:8080/", requestContent);
response.EnsureSuccessStatusCode();

var pdfBytes = await response.Content.ReadAsByteArrayAsync();

await File.WriteAllBytesAsync("test_file.pdf", pdfBytes);

About

Microservice to convert HTML into PDF data using a basic web API .

Topics

Resources

License

Stars

Watchers

Forks

Contributors