anowell / wkhtmltopdf-rs

High-level Rust bindings for wkhtmltopdf
MIT License
75 stars 12 forks source link

Add more safe builder methods for setting global and object settings #2

Open anowell opened 8 years ago

anowell commented 8 years ago

and figure out or report upstream other flags that don't seem to work (like outlineDepth)

derrickcope commented 5 years ago

How would you pass other arguments to builder for wkhtmltopdf? Specifically I would like to pass --user-style-sheet

craigmayhew commented 3 years ago

@anowell could you please confirm the desired way to add command line arguments that don't have a safe rust function.

Is using fn global_setting() the way to go about this?

let html = r##"<!DOCTYPE html><html><head><meta charset="utf-8"></head><body>Hello World</body></html>"##

unsafe {
    let pdf_app = PdfApplication::new().expect("Failed to init PDF application");
    let mut pdfout = pdf_app
        .builder()
        .global_setting("encoding","UTF-8")
        .build_from_html(&html)
        .expect("failed to build pdf");
}
anowell commented 3 years ago

I think global_settings would only work for the fields defined on the PdfGlobal object in the upstream project (also documented as Pdf Global Settings which includes the load.* settings).

The web settings (e.g. encoding and stylesheet) appear to be on the PdfObject. So while I haven't tested it, I'd expect to use: .object_setting("web.defaultEncoding", "UTF-8") and .object_setting("web.userStyleSheet") to accomplish the questions in this issue.

And if those are correct, it should be pretty trivial to add the safe builder methods:

  pub fn default_encoding(&mut self, encoding: &str) -> &mut PdfBuilder {
        self.os.insert("web.defaultEncoding", encoding.to_string().into());
        self
    } 

  pub fn user_style_sheet<P: AsRef<Path>>(&mut self, style_sheet: P) -> &mut PdfBuilder {
        self.os.insert("web.userStyleSheet", style_sheet.as_ref().to_string().into());
        self
    }