pdf_from_html

Function pdf_from_html 

Source
pub async fn pdf_from_html(
    pool: Data<SharedPool>,
    body: Json<PdfFromHtmlRequest>,
) -> impl Responder
Expand description

Generate PDF from HTML content.

This handler converts HTML content directly to PDF without requiring a web server to host the HTML.

§Endpoint

POST /pdf/html
Content-Type: application/json

§Request Body

{
    "html": "<html><body><h1>Hello World</h1></body></html>",
    "filename": "document.pdf",
    "waitsecs": 2,
    "landscape": false,
    "download": false,
    "print_background": true
}
FieldTypeRequiredDefaultDescription
htmlstringYes-HTML content to convert
filenamestringNo"document.pdf"Output filename
waitsecsu64No2Seconds to wait for JavaScript
landscapeboolNofalseUse landscape orientation
downloadboolNofalseForce download vs inline display
print_backgroundboolNotrueInclude background graphics

§Response

Same as pdf_from_url.

§Errors

StatusCodeDescription
400EMPTY_HTMLHTML content is empty or whitespace
502PDF_GENERATION_FAILEDFailed to generate PDF
503BROWSER_UNAVAILABLENo browsers available
504TIMEOUTOperation timed out

§Example Request

curl -X POST http://localhost:8080/pdf/html \
  -H "Content-Type: application/json" \
  -d '{"html": "<h1>Hello</h1>", "filename": "hello.pdf"}' \
  --output hello.pdf

§Usage in App

App::new()
    .app_data(web::Data::new(pool.clone()))
    .route("/pdf/html", web::post().to(pdf_from_html))