pub async fn pdf_from_html(
pool: &State<SharedPool>,
body: Json<PdfFromHtmlRequest>,
) -> HandlerResult<PdfResponder>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
}| Field | Type | Required | Default | Description |
|---|---|---|---|---|
html | string | Yes | - | HTML content to convert |
filename | string | No | "document.pdf" | Output filename |
waitsecs | u64 | No | 2 | Seconds to wait for JavaScript |
landscape | bool | No | false | Use landscape orientation |
download | bool | No | false | Force download vs inline display |
print_background | bool | No | true | Include background graphics |
§Response
Same as pdf_from_url.
§Errors
| Status | Code | Description |
|---|---|---|
| 400 | EMPTY_HTML | HTML content is empty or whitespace |
| 502 | PDF_GENERATION_FAILED | Failed to generate PDF |
| 503 | BROWSER_UNAVAILABLE | No browsers available |
| 504 | TIMEOUT | Operation timed out |
§Example Request
curl -X POST http://localhost:8000/pdf/html \
-H "Content-Type: application/json" \
-d '{"html": "<h1>Hello</h1>", "filename": "hello.pdf"}' \
--output hello.pdf§Usage in App
ⓘ
rocket::build()
.manage(pool)
.mount("/", routes![pdf_from_html])