pdf_from_url

Function pdf_from_url 

Source
pub async fn pdf_from_url(
    pool: &State<SharedPool>,
    query: PdfFromUrlQuery,
) -> HandlerResult<PdfResponder>
Expand description

Generate PDF from a URL.

This handler converts a web page to PDF using the browser pool.

§Endpoint

GET /pdf?url=https://example.com&filename=output.pdf

§Query Parameters

ParameterTypeRequiredDefaultDescription
urlstringYes-URL to convert (must be valid HTTP/HTTPS)
filenamestringNo"document.pdf"Output filename
waitsecsu64No5Seconds to wait for JavaScript
landscapeboolNofalseUse landscape orientation
downloadboolNofalseForce download vs inline display
print_backgroundboolNotrueInclude background graphics

§Response

§Success (200 OK)

Returns PDF binary data with headers:

  • Content-Type: application/pdf
  • Content-Disposition: inline; filename="document.pdf" (or attachment if download=true)
  • Cache-Control: no-cache

§Errors

StatusCodeDescription
400INVALID_URLURL is empty or malformed
502NAVIGATION_FAILEDFailed to load the URL
503BROWSER_UNAVAILABLENo browsers available in pool
504TIMEOUTOperation timed out

§Examples

§Basic Request

GET /pdf?url=https://example.com

§With Options

GET /pdf?url=https://example.com/report&filename=report.pdf&landscape=true&waitsecs=10

§Force Download

GET /pdf?url=https://example.com&download=true&filename=download.pdf

§Usage in App

rocket::build()
    .manage(pool)
    .mount("/", routes![pdf_from_url])