pub struct PdfResponse {
pub data: Vec<u8>,
pub filename: String,
pub force_download: bool,
}Expand description
Successful PDF generation result.
Contains the generated PDF binary data along with metadata for building the HTTP response. This type is returned by the core service functions and converted to framework-specific responses by the integrations.
§Fields
| Field | Type | Description |
|---|---|---|
data | Vec<u8> | Raw PDF binary data |
filename | String | Suggested filename for download |
force_download | bool | Whether to force download vs inline display |
§HTTP Response Headers
When converted to an HTTP response, this generates:
Content-Type: application/pdf
Content-Disposition: inline; filename="document.pdf" (or attachment if force_download)
Cache-Control: no-cache§Examples
use html2pdf_api::service::PdfResponse;
let response = PdfResponse::new(
vec![0x25, 0x50, 0x44, 0x46], // PDF magic bytes
"report.pdf".to_string(),
false, // inline display
);
assert_eq!(response.content_disposition(), "inline; filename=\"report.pdf\"");Fields§
§data: Vec<u8>The generated PDF as raw binary data.
This is the complete PDF file content, ready to be sent as the HTTP response body or written to a file.
§PDF Structure
Valid PDF data always starts with %PDF- (bytes 25 50 44 46 2D).
You can verify the data is valid by checking this header:
fn is_valid_pdf(data: &[u8]) -> bool {
data.starts_with(b"%PDF-")
}filename: StringSuggested filename for the PDF download.
This is used in the Content-Disposition header. The actual
filename used by the browser may differ based on user settings
or browser behavior.
force_download: boolWhether to force download instead of inline display.
true: UsesContent-Disposition: attachment(forces download)false: UsesContent-Disposition: inline(displays in browser)
Implementations§
Source§impl PdfResponse
impl PdfResponse
Sourcepub fn new(data: Vec<u8>, filename: String, force_download: bool) -> Self
pub fn new(data: Vec<u8>, filename: String, force_download: bool) -> Self
Creates a new PDF response.
§Arguments
data- The raw PDF binary datafilename- Suggested filename for the downloadforce_download- Whether to force download vs inline display
§Examples
use html2pdf_api::service::PdfResponse;
// For inline display
let response = PdfResponse::new(
vec![0x25, 0x50, 0x44, 0x46],
"invoice.pdf".to_string(),
false,
);
// For forced download
let response = PdfResponse::new(
vec![0x25, 0x50, 0x44, 0x46],
"confidential-report.pdf".to_string(),
true,
);Sourcepub fn content_disposition(&self) -> String
pub fn content_disposition(&self) -> String
Generates the Content-Disposition header value.
Returns a properly formatted header value based on the
force_download setting.
§Returns
"attachment; filename=\"{filename}\""ifforce_downloadistrue"inline; filename=\"{filename}\""ifforce_downloadisfalse
§Examples
use html2pdf_api::service::PdfResponse;
let inline = PdfResponse::new(vec![], "doc.pdf".to_string(), false);
assert_eq!(inline.content_disposition(), "inline; filename=\"doc.pdf\"");
let download = PdfResponse::new(vec![], "doc.pdf".to_string(), true);
assert_eq!(download.content_disposition(), "attachment; filename=\"doc.pdf\"");Trait Implementations§
Source§impl Clone for PdfResponse
impl Clone for PdfResponse
Source§fn clone(&self) -> PdfResponse
fn clone(&self) -> PdfResponse
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for PdfResponse
impl RefUnwindSafe for PdfResponse
impl Send for PdfResponse
impl Sync for PdfResponse
impl Unpin for PdfResponse
impl UnwindSafe for PdfResponse
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);