### Example: Get PdfBitmap Format Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-bitmap.md Demonstrates how to retrieve and print the pixel format of a PdfBitmap. Ensure the bitmap is valid and error handling is in place. ```rust let format = bitmap.format()?; println!("Format: {:?}", format); ``` -------------------------------- ### Build WASM Module for Example Source: https://github.com/ajrcarey/pdfium-render/blob/master/examples/README.md This command builds the WASM module for the example application using wasm-pack. It targets a no-modules environment, suitable for direct inclusion in HTML. ```bash cargo install wasm-pack && wasm-pack build examples/ --target no-modules ``` -------------------------------- ### Example: Get Raw Bitmap Bytes Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-bitmap.md Demonstrates how to access the raw byte data of a PdfBitmap and print its total length. Ensure the bitmap is valid. ```rust let bytes = bitmap.as_raw_bytes(); println!("Total bytes: {}", bytes.len()); ``` -------------------------------- ### Example: Print Length of Full Page Text Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-text.md Shows how to get the full text of a page and print its length. This is useful for verifying text extraction. ```rust let full_text = text.all(); println!("Text length: {}", full_text.len()); ``` -------------------------------- ### Configuration Setup Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/COMPLETION_REPORT.md Shows how to set up and configure the pdf-render library. This includes setting options and environment variables. ```javascript const pdfRender = require('pdfium-render'); pdfRender.configure({ // configuration options }); ``` -------------------------------- ### Create Page at Start Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-pages.md Prepends a new page with specified dimensions to the beginning of the document. ```APIDOC ## PdfPages::create_page_at_start(paper_size) ### Description Creates a new page at the beginning of the document. ### Method `create_page_at_start(paper_size: &PdfPagePaperSize)` ### Parameters #### Request Body - **paper_size** (`&PdfPagePaperSize`) - Required - Standard paper size ### Return `Result, PdfiumError>` ``` -------------------------------- ### PdfColor Construction Example Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-color.md Demonstrates creating opaque red and semi-transparent PdfColor instances using the standard constructor. ```rust use pdfium_render::prelude::*; let red = PdfColor::new(255, 0, 0, 255); // Opaque red let transparent = PdfColor::new(255, 0, 0, 128); // Semi-transparent ``` -------------------------------- ### Rust Code Example Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/README.md This is a placeholder for Rust code examples. Specific usage patterns, error handling, common workflows, edge cases, and integration patterns are demonstrated. ```rust // Rust code ``` -------------------------------- ### PdfColor From Hexadecimal Example Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-color.md Shows how to create PdfColor instances from hexadecimal strings for red and semi-transparent blue. ```rust let red = PdfColor::from_hex("#FF0000")?; // Red let blue = PdfColor::from_hex("#0000FFAA")?; // Semi-transparent blue ``` -------------------------------- ### Iterate Over Document Pages Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-pages.md A basic example demonstrating how to iterate over all pages in a PDF document. ```rust for page in document.pages().iter() { // Process page } ``` -------------------------------- ### Load PDF from File and Get Version Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-document.md Demonstrates loading a PDF document from a file path and retrieving its version. Ensure the 'doc.pdf' file exists and is accessible. ```rust use pdfium_render::prelude::*; let pdfium = Pdfium::default(); let document = pdfium.load_pdf_from_file("doc.pdf", None)?; println!("Version: {:?}", document.version()); ``` -------------------------------- ### Creating PDF Documents Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/COMPLETION_REPORT.md Shows the basic structure for creating a new PDF document. This is a starting point for adding content. ```javascript const pdf = await pdfRender.createDocument(); ``` -------------------------------- ### Create New Render Config with Defaults Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-render-config.md Creates a new PdfRenderConfig with sensible default settings. Use this as a starting point for further configuration. ```rust use pdfium_render::prelude::*; let config = PdfRenderConfig::new() .set_target_width(1024) .set_maximum_height(1024); ``` -------------------------------- ### Library Initialization and Binding Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/MANIFEST.txt Covers static, dynamic, and WASM binding for the pdfium library, along with document loading from various sources and font provider setup. ```APIDOC ## Library Initialization (pdfium.md) ### Description Provides functionality for initializing the pdfium library, establishing bindings (static, dynamic, WASM), loading PDF documents from files, byte arrays, or custom readers, creating new PDF documents, and setting up a font provider. ### Method Not applicable (Rust library functions) ### Endpoint Not applicable (Rust library functions) ### Parameters - **Binding Type**: (static, dynamic, WASM) - Specifies the type of binding to use. - **Document Source**: (file, bytes, reader) - Specifies the source of the PDF document. - **Font Provider**: (setup) - Configuration for font handling. ### Request Example ```rust // Example of library initialization and document loading // (Specific function signatures would be detailed in the actual documentation) ``` ### Response - **Initialization Status**: (Success/Failure) - Indicates if the library was initialized successfully. - **Document Handle**: (Handle) - A reference to the loaded or created PDF document. #### Response Example ```rust // Example of a successful document load // (Specific return types would be detailed in the actual documentation) ``` ``` -------------------------------- ### Initialize PDFium and pdfium-render WASM Modules Source: https://github.com/ajrcarey/pdfium-render/blob/master/examples/index.html This code initializes the Pdfium Emscripten-wrapped WASM module and the pdfium-render WASM module. It then calls the initialize_pdfium_render function to bind the two modules together. This setup is required before using any Pdfium functionality. ```javascript PDFiumModule().then(async pdfiumModule => { const { initialize_pdfium_render, log_page_metrics_to_console, get_image_data_for_page } = wasm_bindgen; wasm_bindgen('pdfium_render_wasm_example_bg.wasm').then(async rustModule => { console.assert( initialize_pdfium_render( pdfiumModule, rustModule, false ), "Initialization of pdfium-render failed!" ); const targetDocument = "./test.pdf"; await log_page_metrics_to_console(targetDocument); const pageIndex = 0; const width = 1414; const height = 1999; const canvas = document.getElementById("canvas"); canvas.width = width; canvas.height = height; const context = canvas.getContext("2d"); const imageData = await get_image_data_for_page(targetDocument, pageIndex, width, height); context.putImageData(imageData, 0, 0); }); }); ``` -------------------------------- ### Creating Shapes Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/COMPLETION_REPORT.md Provides an example of how to create basic shape objects within a PDF document. This is a foundational step for drawing. ```javascript const shape = pdf.createShape({ x: 10, y: 10, width: 50, height: 50, color: "#FF0000" }); ``` -------------------------------- ### Example: Iterate and Print Text Segments Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-text.md Demonstrates iterating over text segments obtained from a page and printing the text of each segment. Uses `segments()` and `iter()`. ```rust let segments = text.segments()?; for segment in segments.iter() { println!("Segment: {}", segment.text()); } ``` -------------------------------- ### Example Usage of Color Constants in Rust Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-color.md Demonstrates how to use the predefined PdfColor constants for setting text, fill, and stroke colors in your PDF rendering tasks. ```rust let text_color = PdfColor::BLACK; let fill_color = PdfColor::LIGHT_GRAY; let stroke_color = PdfColor::RED; ``` -------------------------------- ### Example: Grouping and Transforming PDF Page Objects Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-objects.md Demonstrates creating a group object and adding other objects to it for collective manipulation. This is typically followed by operations to add objects to the group and then transform the group. ```rust let mut group = page.objects_mut().create_group_object()?; // Add objects to group and manipulate them together ``` -------------------------------- ### Example: Extract and Print All Page Text Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-text.md Demonstrates how to obtain a `PdfPageText` object and print its entire text content. Requires a `page` object and the `pdfium-render` crate. ```rust use pdfium_render::prelude::* let text = page.text()?; println!("Page text: {}", text.all()); ``` -------------------------------- ### Get Specific Pages from Document Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-pages.md Demonstrates how to retrieve the first, last, and a middle page from a document's pages collection. ```rust let first = document.pages().first()?; let last = document.pages().last()?; let middle = document.pages().get(document.pages().len() as c_int / 2)? ``` -------------------------------- ### Bind to System Library Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdfium.md Initializes the external Pdfium library, loading it from system libraries or a WASM module. Use this when Pdfium is installed as a system dependency or for WASM targets. ```rust use pdfium_render::prelude::*; let bindings = Pdfium::bind_to_system_library()?; let pdfium = Pdfium::new(bindings); ``` -------------------------------- ### Create New Page at Start of Document Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-pages.md Use `create_page_at_start()` to add a new page with a specified paper size to the beginning of the PDF document. This operation requires mutable access to the document's pages. ```rust let mut document = pdfium.create_new_pdf()?; let page = document.pages_mut() .create_page_at_start(PdfPagePaperSize::a4())?; ``` -------------------------------- ### Example: Convert Bitmap to RGBA Bytes Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-bitmap.md Shows how to convert a PdfBitmap to RGBA byte format. Each group of 4 bytes in the resulting vector represents one RGBA pixel. Error handling is crucial. ```rust let rgba_data = bitmap.as_rgba_bytes()?; // Each 4 bytes represents one RGBA pixel ``` -------------------------------- ### Iterate and Print Character Unicode Values Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-text.md This example shows how to iterate through all characters on a page and print their Unicode code points in hexadecimal format. ```rust for char_obj in text.chars()?.iter() { let code = char_obj.unicode(); println!("Character U+{:04X}", code); } ``` -------------------------------- ### Get All Text from a Page Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-text.md Retrieves the entire text content of a PDF page as a single string. Useful for full-text indexing or display. ```rust pub fn all(&self) -> String ``` -------------------------------- ### Get PdfBitmap Height Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-bitmap.md Retrieves the pixel height of the bitmap. No setup is required. ```rust pub fn height(&self) -> Pixels ``` -------------------------------- ### Get PdfBitmap Width Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-bitmap.md Retrieves the pixel width of the bitmap. No setup is required. ```rust pub fn width(&self) -> Pixels ``` -------------------------------- ### Example: Extract and Print Text within a Region Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-text.md Illustrates extracting text from a specific rectangular area on a page. Requires creating a `PdfRect` and calling `all_for_rect`. ```rust let region = PdfRect::new(100.0, 100.0, 500.0, 500.0)?; let text_in_region = page.text()?.all_for_rect(®ion)?; ``` -------------------------------- ### Get PdfBitmap Stride Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-bitmap.md Retrieves the byte offset between consecutive scanlines in the bitmap's buffer. No setup is required. ```rust pub fn stride(&self) -> Pixels ``` -------------------------------- ### Build for Specific PDFium API Version Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/configuration.md Specify a particular Pdfium API version using Cargo features. This example builds for version 7543. ```toml pdfium-render = { version = "0.9", default-features = false, features = ["pdfium_7543", "image_latest", "thread_safe"] } ``` -------------------------------- ### Initialize Pdfium with Default Configuration Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/configuration.md Creates a Pdfium library instance using default initialization settings. Requires binding to the system library first. ```rust use pdfium_render::prelude::*; let config = PdfiumLibraryConfig::default(); let bindings = Pdfium::bind_to_system_library()?; let pdfium = Pdfium::new_with_config(bindings, config); ``` -------------------------------- ### Get First Page of PDF Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-pages.md Retrieve the first page of a PDF document using the `first()` method. This method returns an error if the document contains no pages. ```rust let first_page = document.pages().first()?; ``` -------------------------------- ### Load and Render a PDF Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/INDEX.md Initializes Pdfium, loads a PDF document from a file, renders the first page with specified dimensions, and saves it as a PNG image. Ensure 'document.pdf' exists and the library has write permissions for 'page.png'. ```rust use pdfium_render::prelude::*; fn render_pdf() -> Result<(), PdfiumError> { // Initialize Pdfium let pdfium = Pdfium::default(); // Load document let document = pdfium.load_pdf_from_file("document.pdf", None)?; // Get first page let page = document.pages().get(0)?; // Render with configuration let config = PdfRenderConfig::new() .set_target_width(1024) .set_maximum_height(1024); let bitmap = page.render_with_config(&config)?; // Convert to image and save let image = bitmap.as_image()?; image.save("page.png")?; Ok(()) } ``` -------------------------------- ### Pdfium::new(bindings) Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdfium.md Creates a new `Pdfium` instance from external library bindings and initializes the Pdfium library. ```APIDOC ## Pdfium::new(bindings) ### Description Creates a new `Pdfium` instance from external library bindings. Initializes the Pdfium library. ### Method `pub fn new(bindings: Box) -> Self` ### Parameters #### Path Parameters - **bindings** (`Box`) - Required - Bindings to Pdfium functions. ### Return `Pdfium` - A new Pdfium instance. ### Panics If bindings were already initialized. ### Example ```rust use pdfium_render::prelude::*; let bindings = Pdfium::bind_to_system_library()?; let pdfium = Pdfium::new(bindings); ``` ``` -------------------------------- ### Iterate and Print Characters within a Rectangle Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-text.md This example demonstrates how to define a rectangular region and then iterate through all characters found within that region, printing their Unicode values. ```rust let region = PdfRect::new(100.0, 100.0, 500.0, 500.0)?; let chars = text.chars_inside_rect(®ion)?; for char_obj in chars.iter() { println!("Char: {}", char_obj.unicode()); } ``` -------------------------------- ### Get Character by Index Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-text.md The `get()` method allows retrieving a specific character from a `PdfPageTextChars` collection using its index. An out-of-bounds index will result in an error. ```rust pub fn get(&self, index: usize) -> Result, PdfiumError> ``` -------------------------------- ### Get Characters for a Specific Text Object Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-text.md Use `chars_for_object()` to get characters associated with a particular text object on the page. This allows for more granular text retrieval. ```rust pub fn chars_for_object(&self, object: &PdfPageTextObject) -> Result, PdfiumError> ``` -------------------------------- ### Get Specific Page by Index Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-pages.md Access a specific page from the document using its zero-based index with the `get()` method. Handles potential errors if the index is out of bounds. ```rust match document.pages().get(0) { Ok(page) => println!("Got page {}", page.index()), Err(e) => eprintln!("Error: {:?}", e), } ``` -------------------------------- ### Serve Release Files with Basic HTTP Server Source: https://github.com/ajrcarey/pdfium-render/blob/master/examples/README.md This script provides a simple way to serve the compiled WASM application and associated files for local development. It uses the 'basic-http-server' crate. ```bash ./serve.sh ``` -------------------------------- ### Rust: Integrate with Anyhow for Error Handling Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/errors.md Demonstrates how to use the pdfium-render library within an `anyhow::Result` context. This simplifies error propagation in applications. ```rust use anyhow::Result; fn process_pdf(path: &str) -> Result<()> { let pdfium = Pdfium::default()?; let document = pdfium.load_pdf_from_file(path, None)?; Ok(()) } ``` -------------------------------- ### Get Page Orientation Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page.md Retrieves the orientation of a page (Portrait or Landscape). ```rust pub fn orientation(&self) -> PdfPageOrientation ``` -------------------------------- ### Porting C++ Pdfium Initialization to Rust Source: https://github.com/ajrcarey/pdfium-render/blob/master/README.md Demonstrates the equivalent Rust code for initializing and destroying the Pdfium library, loading and closing a document, using the raw FFI bindings. ```cpp string test_doc = "test.pdf"; FPDF_InitLibrary(); FPDF_DOCUMENT doc = FPDF_LoadDocument(test_doc, NULL); // ... do something with doc FPDF_CloseDocument(doc); FPDF_DestroyLibrary(); ``` ```rust let pdfium = Pdfium::default(); let bindings = pdfium.bindings(); let test_doc = "test.pdf"; unsafe { bindings.FPDF_InitLibrary(); let doc = bindings.FPDF_LoadDocument(test_doc, None); // ... do something with doc bindings.FPDF_CloseDocument(doc); bindings.FPDF_DestroyLibrary(); } ``` -------------------------------- ### PdfPageTextSegment::text() Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-text.md Gets the string content of a specific text segment. ```APIDOC ## PdfPageTextSegment::text() ### Description Returns the text content of the segment. ### Method Rust function call ### Parameters None ### Return `String` ``` -------------------------------- ### Get All Page Sizes Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-pages.md Retrieves the dimensions for all pages within the document. ```APIDOC ## PdfPages::get_page_sizes() ### Description Returns the dimensions of all pages. ### Method `get_page_sizes()` ### Return `Result, PdfiumError>` ### Example ```rust let sizes = document.pages().get_page_sizes()?; for (i, size) in sizes.iter().enumerate() { println!("Page {}: {}x{}", i, size.width, size.height); } ``` ``` -------------------------------- ### Bind to Library from Path Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdfium.md Initializes the external Pdfium library from a specific file path. Use this when you have a custom location for the Pdfium library file and are not using static linking. ```rust use pdfium_render::prelude::*; use std::path::Path; let path = Path::new("/usr/local/lib/libpdfium.so"); let bindings = Pdfium::bind_to_library(path)?; let pdfium = Pdfium::new(bindings); ``` -------------------------------- ### Get Page Height Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page.md Retrieves the height of a page in PDF points (f64). ```rust pub fn height(&self) -> PdfPoints ``` -------------------------------- ### Get Page Width Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page.md Retrieves the width of a page in PDF points (f64). ```rust pub fn width(&self) -> PdfPoints ``` -------------------------------- ### Create Pdfium Instance with Custom Configuration Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdfium.md Use this function to create a new Pdfium instance with specific library configurations. It requires bindings to Pdfium functions and a PdfiumLibraryConfig object. ```rust use pdfium_render::prelude::* let bindings = Pdfium::bind_to_system_library()?; let config = PdfiumLibraryConfig::default(); let pdfium = Pdfium::new_with_config(bindings, config); ``` -------------------------------- ### Get Character Font Weight Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-text.md The `font_weight()` method returns the `PdfFontWeight` of a `PdfPageTextChar`. ```rust pub fn font_weight(&self) -> PdfFontWeight ``` -------------------------------- ### Loading PDF Documents Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/COMPLETION_REPORT.md Demonstrates how to load PDF documents using the library. Ensure the file path is correct. ```javascript const pdf = await pdfRender.loadDocument("path/to/your/document.pdf"); ``` -------------------------------- ### PdfPageTextSegments::len() Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-text.md Gets the total number of text segments on a PDF page. ```APIDOC ## PdfPageTextSegments::len() ### Description Returns the number of text segments. ### Method Rust function call ### Parameters None ### Return `usize` ``` -------------------------------- ### Create a New PDF Document Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdfium.md Use this function to initialize an empty PDF document. Ensure Pdfium is properly initialized before calling. ```rust pub fn create_new_pdf(&self) -> Result, PdfiumError> ``` ```rust use pdfium_render::prelude::*; let pdfium = Pdfium::default(); let document = pdfium.create_new_pdf()?; ``` -------------------------------- ### Get Character Stroke Color Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-text.md The `stroke_color()` method returns the `PdfColor` used for stroking the character. ```rust pub fn stroke_color(&self) -> Result ``` -------------------------------- ### Pdfium::default() Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdfium.md Creates a new Pdfium instance by automatically attempting to load the Pdfium library from common system locations. ```APIDOC ## Pdfium::default() ### Description Automatically creates a new `Pdfium` instance by attempting to load the library from: 1. The current working directory 2. System library locations ### Method `Default::default()` ### Parameters No parameters. ### Return `Self` - A new `Pdfium` instance. ### Example ```rust use pdfium_render::prelude::*; let pdfium = Pdfium::default(); ``` ``` -------------------------------- ### Get Character Fill Color Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-text.md The `fill_color()` method returns the `PdfColor` used for filling the character. ```rust pub fn fill_color(&self) -> Result ``` -------------------------------- ### Create a New PDF Document Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/INDEX.md Creates a new, empty PDF document, adds a page with A4 dimensions, inserts "Hello, PDF!" text using the Helvetica font, and saves the document to 'output.pdf'. ```rust use pdfium_render::prelude::*; let pdfium = Pdfium::default(); let mut document = pdfium.create_new_pdf()?; // Add a page let page = document.pages_mut() .create_page_at_end(PdfPagePaperSize::a4())?; // Add text let font = PdfFont::builtin(PdfFontBuiltin::Helvetica)?; page.objects_mut() .create_text_object("Hello, PDF!", font)?; // Save document.save_to_file("output.pdf")?; ``` -------------------------------- ### Get Character Font Information Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-text.md The `font()` method returns the `PdfFont` object associated with a `PdfPageTextChar`. ```rust pub fn font(&self) -> Result, PdfiumError> ``` -------------------------------- ### Create PDF Document with Different Fonts Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-font.md Demonstrates creating a new PDF document and adding text objects with different built-in fonts (bold, regular, monospace). Ensure the pdfium-render crate is added as a dependency. ```rust use pdfium_render::prelude::*; fn create_document_with_fonts() -> Result<(), PdfiumError> { let pdfium = Pdfium::default(); let mut document = pdfium.create_new_pdf()?; let mut page = document.pages_mut() .create_page_at_end(PdfPagePaperSize::a4())?; // Add title in bold let bold = PdfFont::builtin(PdfFontBuiltin::HelveticaBold)?; page.objects_mut().create_text_object("Document Title", bold)?; // Add body text let regular = PdfFont::builtin(PdfFontBuiltin::Helvetica)?; page.objects_mut().create_text_object("Body content here", regular)?; // Add code in monospace let mono = PdfFont::builtin(PdfFontBuiltin::Courier)?; page.objects_mut().create_text_object("code_here()", mono)?; document.save_to_file("document.pdf")?; Ok(()) } ``` -------------------------------- ### Get Character Unicode Value Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-text.md The `unicode()` method returns the Unicode code point of a `PdfPageTextChar`. ```rust pub fn unicode(&self) -> u32 ``` -------------------------------- ### Get Character Font Size Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-text.md The `font_size()` method returns the font size of a `PdfPageTextChar` in PDF points. ```rust pub fn font_size(&self) -> PdfPoints ``` -------------------------------- ### Create Default Pdfium Instance Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdfium.md Creates a new Pdfium instance. The library is automatically loaded from common system locations. ```rust use pdfium_render::prelude::*; let pdfium = Pdfium::default(); ``` -------------------------------- ### Get Character Count Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-text.md The `len()` method returns the total number of characters available in a `PdfPageTextChars` collection. ```rust pub fn len(&self) -> usize ``` -------------------------------- ### Get Font Glyphs Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-font.md Retrieves the collection of glyphs available in a font. Use this to access all glyphs for further processing. ```rust let font = PdfFont::builtin(PdfFontBuiltin::Helvetica)?; let glyphs = font.glyphs()?; println!("Font has {} glyphs", glyphs.len()); ``` -------------------------------- ### Get Font Family Name Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-font.md Retrieve the font family name. This is useful for identifying the font type. ```rust let font = PdfFont::builtin(PdfFontBuiltin::Helvetica)?; assert_eq!(font.family(), "Helvetica"); ``` -------------------------------- ### Configure and Perform Text Search Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-text.md Demonstrates how to set up search options and execute a text search on a PDF page. Use this to find specific text occurrences. ```rust let options = PdfSearchOptions::default() .search_term("hello") .search_direction(PdfSearchDirection::Forward); let search = text.search(&options)?; for result in search.iter() { println!("Found: {}", result.text()); } ``` -------------------------------- ### Get PDF Page Rotation Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page.md Use `PdfPage::rotation` to retrieve the current rotation setting of a PDF page. ```rust let current_rotation = page.rotation(); ``` -------------------------------- ### Get Page Index Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page.md Retrieves the zero-based index of a page within the PDF document. Useful for iterating through pages. ```rust pub fn index(&self) -> PdfPageIndex ``` ```rust for page in document.pages().iter() { println!("Page {}", page.index()); } ``` -------------------------------- ### Pdfium::new_with_config Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdfium.md Creates a new Pdfium instance with custom library configuration, allowing for specific bindings and settings. ```APIDOC ## `Pdfium::new_with_config(bindings, config)` ### Description Creates a new `Pdfium` instance with custom library configuration. ### Parameters - **bindings**: `Box` - Bindings to Pdfium functions - **config**: `PdfiumLibraryConfig` - Custom library configuration ### Return - `Pdfium` - A new Pdfium instance with applied configuration ### Example ```rust use pdfium_render::prelude::*; let bindings = Pdfium::bind_to_system_library()?; let config = PdfiumLibraryConfig::default(); let pdfium = Pdfium::new_with_config(bindings, config); ``` ``` -------------------------------- ### Get Individual Characters in a Segment Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-text.md Retrieves the individual characters that make up a text segment. This allows for character-level processing. ```rust pub fn chars(&self) -> Result, PdfiumError> ``` -------------------------------- ### Get PdfBitmap Format Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-bitmap.md Retrieves the pixel format of the bitmap. This method returns a Result, so error handling is necessary. ```rust pub fn format(&self) -> Result ``` -------------------------------- ### Web Display Configuration Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-render-config.md Configure rendering for web display by setting target width and maximum height, and optionally rotating if the page is landscape. ```rust use pdfium_render::prelude::*; let config = PdfRenderConfig::new() .set_target_width(1024) .set_maximum_height(1024) .rotate_if_landscape(PdfPageRenderRotation::Degrees90, true); let bitmap = page.render_with_config(&config)?; let image = bitmap.as_image()?; image.save("page.png")?; ``` -------------------------------- ### Pdfium::bind_to_system_library() Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdfium.md Initializes the external Pdfium library, loading it from system libraries (native) or a WASM module (when compiling to WASM). ```APIDOC ## Pdfium::bind_to_system_library() ### Description Initializes the external Pdfium library, loading it from system libraries (native) or WASM module (when compiling to WASM). ### Method `pub fn bind_to_system_library() -> Result, PdfiumError>` ### Parameters No parameters. ### Return - Success: `Result, PdfiumError>` - Returns bindings to system library functions. - Error: `PdfiumError` - If the library is not found or already initialized. ### Example ```rust use pdfium_render::prelude::*; let bindings = Pdfium::bind_to_system_library()?; let pdfium = Pdfium::new(bindings); ``` ``` -------------------------------- ### Loading and Using a Custom Font from a File Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-font.md Shows how to load a font from a TTF file, add it to a page with custom text, and access its properties. ```rust // Load from file let custom = PdfFont::from_file("fonts/myfont.ttf", None)?; page.objects_mut().create_text_object("Custom text", custom)?; // Check properties println!("Family: {}", custom.family()); println!("Embedded: {}", custom.is_embedded()); ``` -------------------------------- ### Get Bounding Box of a Text Segment Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-text.md Obtains the rectangular coordinates defining the location of a text segment on the page. Returns a `PdfRect` object. ```rust pub fn bounds(&self) -> Result ``` -------------------------------- ### Get Text Content of a Segment Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-text.md Retrieves the string content of an individual text segment. This represents a logical block of text, like a line. ```rust pub fn text(&self) -> String ``` -------------------------------- ### Rendering Configuration Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/MANIFEST.txt Allows configuration of rendering options using a builder pattern. ```APIDOC ## Rendering Configuration (pdf-render-config.md) ### Description Provides a builder pattern for configuring rendering options, allowing fine-grained control over how PDF pages are rendered. ### Method Not applicable (Rust library functions) ### Endpoint Not applicable (Rust library functions) ### Parameters - **Configuration Options**: (Various types) - Options such as scale, rotation, color format, etc. ### Request Example ```rust // Example of building a render configuration // (Specific function signatures would be detailed in the actual documentation) ``` ### Response - **Render Configuration Object**: (Config) - The constructed rendering configuration. #### Response Example ```rust // Example of a render configuration object // (Specific return types would be detailed in the actual documentation) ``` ``` -------------------------------- ### Bind to Pdfium Library with Fallback to System Library Source: https://github.com/ajrcarey/pdfium-render/blob/master/README.md This snippet shows a common pattern for binding to Pdfium. It first attempts to bind to a library in the current directory and falls back to a system-provided library if the first attempt fails. Error handling is managed with `or_else` and `unwrap`. ```rust use pdfium_render::prelude::*; let pdfium = Pdfium::new( Pdfium::bind_to_library(Pdfium::pdfium_platform_library_name_at_path("./")) .or_else(|_| Pdfium::bind_to_system_library()) .unwrap() // Or use the ? unwrapping operator to pass any error up to the caller ); ``` -------------------------------- ### Get Text Segments from a Page Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-text.md Retrieves text organized into segments, typically lines or text runs. This is useful for structured text analysis. ```rust pub fn segments(&self) -> Result, PdfiumError> ``` -------------------------------- ### Pdfium::bind_to_library(path) Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdfium.md Initializes the external Pdfium library from a specific file path. This is available when not using static linking. ```APIDOC ## Pdfium::bind_to_library(path) ### Description Initializes the external Pdfium library from a specific file path. Available only when not using static linking. ### Method `pub fn bind_to_library(path: impl AsRef) -> Result, PdfiumError>` ### Parameters #### Path Parameters - **path** (`impl AsRef`) - Required - Path to the Pdfium library file. ### Return - Success: `Result, PdfiumError>` - Returns bindings to the library at the specified path. - Error: `PdfiumError` - If the library cannot be loaded or is already initialized. ### Example ```rust use pdfium_render::prelude::*; use std::path::Path; let path = Path::new("/usr/local/lib/libpdfium.so"); let bindings = Pdfium::bind_to_library(path)?; let pdfium = Pdfium::new(bindings); ``` ``` -------------------------------- ### Get Pdfium Library Bindings Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdfium.md Retrieves a reference to the underlying Pdfium library bindings. This is useful for direct interaction with the FFI layer if needed. ```rust use pdfium_render::prelude::*; let pdfium = Pdfium::default(); let bindings = pdfium.bindings(); ``` -------------------------------- ### Get Page Size Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-pages.md Retrieves the dimensions (width and height) of a PDF page in PDF points. Use this to determine the physical size of the page. ```rust pub struct PdfPageSize { pub width: PdfPoints, pub height: PdfPoints, } ``` ```rust let size = page.size(); println!("Width: {} points ({:.1} inches)", size.width, size.width / 72.0); ``` -------------------------------- ### Thumbnail Generation Configuration Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-render-config.md Configure rendering for thumbnail generation by setting a fixed size and specifying the bitmap format. ```rust let config = PdfRenderConfig::new() .set_fixed_size(200, 200) .set_target_bitmap_format(PdfBitmapFormat::BGRA); let bitmap = page.render_with_config(&config)?; ``` -------------------------------- ### Configure Thumbnail Generation Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/configuration.md Optimize for thumbnail generation by setting a small target width and a BGRA bitmap format, which is efficient for many image processing tasks. ```rust let config = PdfRenderConfig::new() .set_target_width(200) .set_target_bitmap_format(PdfBitmapFormat::BGRA); let bitmap = page.render_with_config(&config)?; ``` -------------------------------- ### Get All Page Sizes Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-pages.md Use `get_page_sizes()` to retrieve a vector containing the dimensions of all pages in the document. This is useful for an overview of the document's layout. ```rust let sizes = document.pages().get_page_sizes()?; for (i, size) in sizes.iter().enumerate() { println!("Page {}: {}x{}", i, size.width, size.height); } ``` -------------------------------- ### Render PDF Page with Configuration Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page.md Use `render_with_config` to render a page to a bitmap with custom settings like target width and maximum height. Ensure `PdfRenderConfig` is properly set up. ```rust use pdfium_render::prelude::*; let config = PdfRenderConfig::new() .set_target_width(1024) .set_maximum_height(1024); let bitmap = page.render_with_config(&config)?; ``` -------------------------------- ### Iterate Over PDF Pages Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-pages.md Use `iter()` to get an iterator for all pages in a document. This is useful for processing each page sequentially, such as extracting text or metadata. ```rust for page in document.pages().iter() { println!("Page {}: {} chars", page.index(), page.text()?.chars()?.len()); } ``` -------------------------------- ### Handling Colors Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/COMPLETION_REPORT.md Demonstrates how to define and use colors for various PDF elements. Supports standard color formats. ```javascript const color = pdf.createColor("#00FF00"); ``` -------------------------------- ### Get Type of PDF Page Object Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-objects.md Retrieves the specific type of a given PDF page object. This can be used to determine how to further process or interpret the object. ```rust pub fn object_type(&self) -> Result ``` -------------------------------- ### Get Text Segment by Index Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-text.md Accesses a specific text segment on the page using its index. The index must be within the valid range of segments. ```rust pub fn get(&self, index: usize) -> Result, PdfiumError> ``` -------------------------------- ### Default Rendering Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/configuration.md Renders a PDF page using the default configuration settings when no specific options are provided. ```rust let bitmap = page.render()?; ``` -------------------------------- ### Iterate Through PDF Pages Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-document.md Use `document.pages().iter()` to get an immutable collection of pages and iterate through them. This is useful for reading page properties like the index. ```rust for page in document.pages().iter() { println!("Page index: {}", page.index()); } ``` -------------------------------- ### Enable Static Linking Feature Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/configuration.md Enable the 'static' Cargo feature to use static linking mode for the pdfium-render crate. ```toml pdfium-render = { version = "0.9", default-features = false, features = ["pdfium_latest", "image_024", "thread_safe"] } ``` -------------------------------- ### Safe Buffer Allocation for PdfBitmap Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/errors.md Shows how to safely allocate a buffer for a `PdfBitmap` by first calculating the required size using `bytes_required_for_size()`. This prevents potential issues with insufficient buffer sizes when rendering bitmaps. ```rust let required = PdfBitmap::bytes_required_for_size(1024, 768, PdfBitmapFormat::BGRA)?; let mut buffer = vec![0u8; required]; let bitmap = PdfBitmap::from_bytes(1024, 768, PdfBitmapFormat::BGRA, &mut buffer)?; ``` -------------------------------- ### Create Default Render Configuration Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/configuration.md Initializes a PdfRenderConfig with default values, which preserve the page's original dimensions and orientation without rotation or clipping. ```rust let config = PdfRenderConfig::new(); ``` -------------------------------- ### Set PDFIUM_STATIC_LIB_PATH for Static Linking Source: https://github.com/ajrcarey/pdfium-render/blob/master/README.md Instructs cargo to link to a statically-built Pdfium library. Set this environment variable to the directory containing your static Pdfium library. ```bash PDFIUM_STATIC_LIB_PATH="/path/containing/your/static/pdfium/library" cargo build ``` ```bash cargo:rustc-link-lib=static=pdfium cargo:rustc-link-search=native=$PDFIUM_STATIC_LIB_PATH ``` -------------------------------- ### Searching Text within a Document Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/COMPLETION_REPORT.md Provides an example of how to search for specific text strings within a loaded PDF document. Returns matches and their locations. ```javascript const results = await pdf.searchText("search term"); ``` -------------------------------- ### Get Object by Index Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-page-objects.md Retrieves a specific graphical object from the page using its zero-based index. Ensure the index is within the valid range to avoid errors. ```rust use pdfium_render::prelude::*; if let Ok(obj) = page.objects().get(0) { println!("Object type: {:?}", obj.object_type()?); } ``` -------------------------------- ### Creating a Multi-Page Document Source: https://github.com/ajrcarey/pdfium-render/blob/master/_autodocs/api-reference/pdf-pages.md Shows how to create a new PDF document and add multiple pages to it. ```APIDOC ## Create Multi-Page Document ### Description This snippet demonstrates the process of creating a new, empty PDF document and then adding several pages to it, specifying the size for each new page. ### Methods - `create_new_pdf()` - `pages_mut()` - `create_page_at_end(size: PdfPagePaperSize)` ### Example ```rust let mut doc = pdfium.create_new_pdf()?; let pages = doc.pages_mut(); for _ in 0..5 { pages.create_page_at_end(PdfPagePaperSize::letter())?; } ``` ```