pub struct PdfFromHtmlRequest {
pub html: String,
pub filename: Option<String>,
pub waitsecs: Option<u64>,
pub landscape: Option<bool>,
pub download: Option<bool>,
pub print_background: Option<bool>,
pub base_url: Option<String>,
}Expand description
Request parameters for converting HTML content to PDF.
This struct represents the request body for the HTML-to-PDF endpoint. The HTML content is loaded via a data URL, so no external server is needed.
§Required Fields
| Field | Type | Description |
|---|---|---|
html | String | Complete HTML document or fragment to convert |
§Optional Fields
| Field | Type | Default | Description |
|---|---|---|---|
filename | Option<String> | "document.pdf" | Output filename |
waitsecs | Option<u64> | 2 | Seconds to wait for JavaScript |
landscape | Option<bool> | false | Use landscape orientation |
download | Option<bool> | false | Force download vs inline |
print_background | Option<bool> | true | Include backgrounds |
base_url | Option<String> | None | Base URL for relative links (not yet implemented) |
§HTML Content Guidelines
§Complete Document (Recommended)
For best results, provide a complete HTML document:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
body { font-family: Arial, sans-serif; }
/* Your styles here */
</style>
</head>
<body>
<h1>Your Content</h1>
<p>Your content here...</p>
</body>
</html>§Fragment
HTML fragments are wrapped automatically, but styling may be limited:
<h1>Hello World</h1>
<p>This is a paragraph.</p>§External Resources
Since HTML is loaded via data URL, external resources have limitations:
| Resource Type | Behavior |
|---|---|
| Inline styles | ✅ Works |
| Inline images (base64) | ✅ Works |
External CSS (<link>) | ⚠️ May work if absolute URL |
| External images | ⚠️ May work if absolute URL |
| Relative URLs | ❌ Will not resolve |
| External fonts | ⚠️ May work if absolute URL |
For reliable results, embed all resources inline or use absolute URLs.
§Examples
§Simple HTML conversion
use html2pdf_api::service::PdfFromHtmlRequest;
let request = PdfFromHtmlRequest {
html: "<h1>Invoice #12345</h1><p>Amount: $99.99</p>".to_string(),
filename: Some("invoice.pdf".to_string()),
..Default::default()
};§Complete document with styling
use html2pdf_api::service::PdfFromHtmlRequest;
let html = r#"
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: 'Helvetica', sans-serif; padding: 20px; }
h1 { color: #333; border-bottom: 2px solid #007bff; }
table { width: 100%; border-collapse: collapse; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
</style>
</head>
<body>
<h1>Monthly Report</h1>
<table>
<tr><th>Item</th><th>Value</th></tr>
<tr><td>Revenue</td><td>$10,000</td></tr>
</table>
</body>
</html>
"#;
let request = PdfFromHtmlRequest {
html: html.to_string(),
filename: Some("report.pdf".to_string()),
landscape: Some(true),
..Default::default()
};§HTTP API Usage
POST /pdf/html
Content-Type: application/json
{
"html": "<!DOCTYPE html><html>...</html>",
"filename": "document.pdf",
"landscape": false,
"download": true
}Fields§
§html: StringHTML content to convert to PDF.
Can be a complete HTML document or a fragment. Complete documents
with proper <!DOCTYPE>, <html>, <head>, and <body> tags
are recommended for consistent rendering.
§Size Limits
While there’s no hard limit, very large HTML documents may:
- Increase processing time
- Consume more memory
- Hit URL length limits (data URLs have browser-specific limits)
For documents over 1MB, consider hosting the HTML and using
PdfFromUrlRequest instead.
filename: Option<String>Output filename for the generated PDF.
Defaults to "document.pdf" if not specified.
See PdfFromUrlRequest::filename for details.
waitsecs: Option<u64>Seconds to wait for JavaScript execution.
Defaults to 2 seconds for HTML content (shorter than URL conversion
since the content is already loaded).
Increase this value if your HTML includes JavaScript that modifies the DOM after initial load.
landscape: Option<bool>Use landscape page orientation.
See PdfFromUrlRequest::landscape for details.
download: Option<bool>Force download instead of inline display.
See PdfFromUrlRequest::download for details.
print_background: Option<bool>Include background colors and images.
See PdfFromUrlRequest::print_background for details.
base_url: Option<String>Base URL for resolving relative links.
Note: This feature is not yet implemented. Relative URLs in HTML content will not resolve correctly. Use absolute URLs for external resources.
§Future Behavior
When implemented, this will allow relative URLs in the HTML to resolve against the specified base:
{
"html": "<img src=\"/images/logo.png\">",
"base_url": "https://example.com"
}Would resolve the image to https://example.com/images/logo.png.
Implementations§
Source§impl PdfFromHtmlRequest
impl PdfFromHtmlRequest
Sourcepub fn filename_or_default(&self) -> String
pub fn filename_or_default(&self) -> String
Returns the filename, using "document.pdf" as the default.
§Examples
use html2pdf_api::service::PdfFromHtmlRequest;
let request = PdfFromHtmlRequest::default();
assert_eq!(request.filename_or_default(), "document.pdf");Sourcepub fn wait_duration(&self) -> Duration
pub fn wait_duration(&self) -> Duration
Returns the JavaScript wait duration.
Defaults to 2 seconds for HTML content (shorter than URL conversion).
§Examples
use html2pdf_api::service::PdfFromHtmlRequest;
use std::time::Duration;
let request = PdfFromHtmlRequest::default();
assert_eq!(request.wait_duration(), Duration::from_secs(2));Sourcepub fn is_download(&self) -> bool
pub fn is_download(&self) -> bool
Returns whether download mode is enabled.
See PdfFromUrlRequest::is_download for details.
Sourcepub fn is_landscape(&self) -> bool
pub fn is_landscape(&self) -> bool
Returns whether landscape orientation is enabled.
See PdfFromUrlRequest::is_landscape for details.
Sourcepub fn print_background(&self) -> bool
pub fn print_background(&self) -> bool
Returns whether background printing is enabled.
See PdfFromUrlRequest::print_background for details.
Trait Implementations§
Source§impl Clone for PdfFromHtmlRequest
impl Clone for PdfFromHtmlRequest
Source§fn clone(&self) -> PdfFromHtmlRequest
fn clone(&self) -> PdfFromHtmlRequest
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for PdfFromHtmlRequest
impl Debug for PdfFromHtmlRequest
Source§impl Default for PdfFromHtmlRequest
impl Default for PdfFromHtmlRequest
Source§fn default() -> PdfFromHtmlRequest
fn default() -> PdfFromHtmlRequest
Source§impl<'de> Deserialize<'de> for PdfFromHtmlRequest
impl<'de> Deserialize<'de> for PdfFromHtmlRequest
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for PdfFromHtmlRequest
impl RefUnwindSafe for PdfFromHtmlRequest
impl Send for PdfFromHtmlRequest
impl Sync for PdfFromHtmlRequest
impl Unpin for PdfFromHtmlRequest
impl UnwindSafe for PdfFromHtmlRequest
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);