Coder Social home page Coder Social logo

wkhtmltopdf-rs's Introduction

wkhtmltopdf-rs

High-level Rust bindings for wkhtmltopdf. This is a wrapper around the low-level binding provided by libwkhtmltox-sys.

Resource Link
Crate Crates.io
Documentation Cargo docs
Upstream wkhtmltopdf.org

This crate aims to provide full configuration of wkhtmltopdf with safe, ergonomic Rust. Wkhtmltopdf has several non-obvious limitations (mostly caused by Qt). that make it very easy to cause undefined behavior with the C bindings. Two such limitations that greatly impact the API are:

  1. Wkhtmltopdf initialization can only occur once per process; deinitialization does make it safe to reuse
  2. PDF generation must always occur on the thread that initialized wkhtmltopdf

This crate should make it impossible to break those rules in safe code. If you need parallel PDF generation, you will need to spawn/fork processes to do so. Such an abstraction would be a welcome addition to this crate.

Install

Install wkhtmltopdf 0.12.3.

Note: This library using the libs (shared objects) and includes (headers) for PDF generation instead of the wkhtmltopdf executable.

Usage

Basic usage looks like this:

  let html = r#"<html><body><div>foo</div></body></html>"#;
  let mut pdf_app = PdfApplication::new().expect("Failed to init PDF application");
  let mut pdfout = pdf_app.builder()
      .orientation(Orientation::Landscape)
      .margin(Size::Inches(2))
      .title("Awesome Foo")
      .build_from_html(&html)
      .expect("failed to build pdf");

  pdfout.save("foo.pdf").expect("failed to save foo.pdf");
  println!("generated PDF saved as: foo.pdf");

Build

As long as the includes are installed (e.g. pdf.h), then it's all cargo:

cargo build
cargo test

Note: tests have to be combined into a single test case because we can only init PdfApplication once, and it is !Send/!Sync. So the preference going forward will be to test with a variety of good examples.

Contributions welcome in the form of issue reports, feature requests, feedback, and/or pull request.

wkhtmltopdf-rs's People

Contributors

anowell avatar sciguy16 avatar aleury avatar craigmayhew avatar leandrosilva avatar icandivideby0 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.