-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alastair Carey
committed
Feb 24, 2022
1 parent
1a0f4a1
commit b37da69
Showing
17 changed files
with
706 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Oops, something went wrong.