Skip to content

Commit

Permalink
Resolves #11
Browse files Browse the repository at this point in the history
  • Loading branch information
Alastair Carey committed Feb 24, 2022
1 parent 1a0f4a1 commit b37da69
Show file tree
Hide file tree
Showing 17 changed files with 706 additions and 76 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pdfium-render"
version = "0.5.4"
version = "0.5.5"
edition = "2018"
publish = true
description = "A high-level idiomatic Rust wrapper around Pdfium, the C++ PDF library used by the Google Chromium project."
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Pdfium exposed by the excellent `pdfium-sys` crate.

```
// Exports each page in the given test PDF file as a separate JPEG image.
// Renders each page in the given test PDF file to a separate JPEG file.
use pdfium_render::{pdfium::Pdfium, bitmap::PdfBitmapRotation, bitmap_config::PdfBitmapConfig};
use image::ImageFormat;
Expand Down Expand Up @@ -120,6 +120,9 @@ If you need a function that is not currently exposed, just raise an issue.

## Version history

* 0.5.5: fixes two bugs in the WASM implementation, one to do with colors, one to do with text extraction.
See <https://github.com/ajrcarey/pdfium-render/issues/9> and <https://github.com/ajrcarey/pdfium-render/issues/11>
for more information.
* 0.5.4: changes default setting of `PdfBitmapConfig::set_reverse_byte_order()` to `true` to switch from
Pdfium's default BGRA8 pixel format to RGBA8. This is necessary since the `image` crate dropped
support for BGRA8 in version 0.24. See <https://github.com/ajrcarey/pdfium-render/issues/9> for more information.
Expand Down
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Simple examples demonstrating how to use `pdfium-render` on both native and WASM

Since `pdfium-render` does not include Pdfium itself, an external pre-packaged WASM build of `pdfium` is required. Suitable builds are available from https://github.com/paulo-coutinho/pdfium-lib/releases.

* Build the WASM module for the sample: `cargo install wasm-pack && wasm-pack build examples/ --target no-modules`. This creates a WASM module and supporting Javascript files in `examples/pkg`.
* Build the WASM module for the sample: `cargo install wasm-pack && wasm-pack build examples/ --target no-modules`. This creates a WASM module and supporting Javascript files in `examples/pkg`.
* Copy the `pdfium_render_wasm_example.js` and `pdfium_render_wasm_example_bg.wasm` files from `examples/pkg/` into a release folder.
* Download a pre-packaged WASM build from https://github.com/paulo-coutinho/pdfium-lib/releases and extract the `release/node/pdfium.js` and `release/node/pdfium.wasm` files into your release folder.
* Copy the `index.html` file from `examples` into your release folder.
Expand Down
18 changes: 14 additions & 4 deletions examples/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ use pdfium_render::page_object::PdfPageObjectCommon;
use pdfium_render::pdfium::Pdfium;

pub fn main() {
// This example differs from export.rs in that we iterate over every page object in each page,
// outputting information about each to the console. Comments that would duplicate
// those in export.rs have been removed; only code that substantively differs is commented.
// For general comments about pdfium-render and binding to Pdfium, see comments in export.rs.

let bindings = Pdfium::bind_to_library(Pdfium::pdfium_platform_library_name_at_path("./"))
.or_else(|_| Pdfium::bind_to_system_library());

match bindings {
Ok(bindings) => {
// For every page in our sample file...

Pdfium::new(bindings)
.load_pdf_from_file("test/export-test.pdf", None) // Load the sample file...
.load_pdf_from_file("test/export-test.pdf", None)
.unwrap()
.pages()
.iter()
.for_each(|page| {
// ... output information about every object on the page to the console.

println!("=============== Page {} ===============", page.index());

page.objects().iter().for_each(|object| {
Expand All @@ -27,6 +29,9 @@ pub fn main() {
object.object_type()
);

// For text objects, we take the extra step of outputting the text
// contained by the object.

if let Some(object) = object.as_text_object() {
println!(
"{} {}-pt: {}",
Expand All @@ -35,6 +40,11 @@ pub fn main() {
object.text()
);
}

// If we wanted to extract _all_ the text contained by all the text objects
// on the page, an easier way would be to simply use

// page.text().unwrap().all()
});
});
}
Expand Down
76 changes: 76 additions & 0 deletions examples/serve/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script src="pdfium.js"></script>
<script src="pdfium_render_wasm_example.js"></script>
</head>

<body>
<canvas id="canvas" style="max-width: 100%; height: auto; border: 1px solid black;"></canvas>

<script>
// Initialize Pdfium's emscripten-wrapped WASM module.

Module.onRuntimeInitialized = async _ => {
// Pdfium's WASM module has now been loaded and initialized. We can now
// bind to any functions exported from our Rust application via the
// #[wasm_bindgen] declaration. In example/wasm.rs there are two such
// functions: log_page_metrics_to_console() and get_image_data_for_page().

// In addition to any functions exported by our Rust application, pdfium-render will
// always export an initialization function, initialize_pdfium_render(),
// which must be called from Javascript prior to the use of any Pdfium functionality.

const {
initialize_pdfium_render,
log_page_metrics_to_console,
get_image_data_for_page
} = wasm_bindgen;

// Load the wasm_bindgen-wrapped WASM module containing our Rust application.

wasm_bindgen('pdfium_render_wasm_example_bg.wasm').then(function () {
// The functions exported by our Rust application are now available.

// First, we call pdfium-render's exported initialize_pdfium_render() function,
// passing in the Pdfium WASM module and, optionally, a debug flag.
// pdfium-render will bind to the functions it needs, returning a boolean value
// indicating success or failure.

const debug = false; // Set this to true to see more debugging information in the Javascript console.

console.assert(
initialize_pdfium_render(Module, debug),
"Initialization of pdfium-render failed!"
);

// Now we can call the Rust functions we exported in example/wasm.rs.
// The first function dumps sizing metrics for each page to the console.

log_page_metrics_to_console();

// The second function generates the raw bitmap byte data for a single page in
// a sample PDF file. We can use this byte data to render the page to an HTML canvas.

const pageIndex = 0;
const width = 1414;
const height = 1999;

const image_bytes = get_image_data_for_page(pageIndex, width, height);

const canvas = document.getElementById("canvas");

canvas.width = width;
canvas.height = height;

const context = canvas.getContext("2d");
const image = context.createImageData(width, height);

image.data.set(image_bytes);
context.putImageData(image, 0, 0);
});
};
</script>
</body>
</html>
1 change: 1 addition & 0 deletions examples/serve/pdfium.js

Large diffs are not rendered by default.

Binary file added examples/serve/pdfium.wasm
Binary file not shown.
Loading

0 comments on commit b37da69

Please # to comment.