#pdf #bindings #safe-bindings #vendored #source #encryption #qpdf

sys qpdf-sys

Rust bindings to QPDF C++ library via FFI and bindgen

10 releases

0.3.0 Apr 26, 2024
0.2.1 Mar 20, 2024
0.1.7 Nov 23, 2023
0.1.6 Jul 24, 2023
0.1.1 Jul 26, 2022

#11 in #vendored

Download history 211/week @ 2024-07-06 127/week @ 2024-07-13 92/week @ 2024-07-20 170/week @ 2024-07-27 128/week @ 2024-08-03 75/week @ 2024-08-10 102/week @ 2024-08-17 188/week @ 2024-08-24 118/week @ 2024-08-31 126/week @ 2024-09-07 147/week @ 2024-09-14 164/week @ 2024-09-21 115/week @ 2024-09-28 194/week @ 2024-10-05 123/week @ 2024-10-12 156/week @ 2024-10-19

609 downloads per month
Used in 2 crates (via qpdf)

MIT/Apache

6.5MB
139K SLoC

C 48K SLoC // 0.2% comments C++ 36K SLoC // 0.1% comments Visual Studio Project 15K SLoC Rust 15K SLoC // 0.0% comments Shell 8K SLoC // 0.2% comments M4 7.5K SLoC // 0.2% comments Assembly 2K SLoC // 0.2% comments Ada 1.5K SLoC // 0.2% comments GNU Style Assembly 1.5K SLoC // 0.3% comments Perl 1.5K SLoC // 0.1% comments Pascal 1K SLoC // 0.2% comments C# 879 SLoC // 0.4% comments Visual Studio Solution 655 SLoC Bitbake 527 SLoC WebAssembly 273 SLoC HICAD 208 SLoC Python 124 SLoC // 0.2% comments Automake 117 SLoC // 0.2% comments Batch 19 SLoC Forge Config 7 SLoC // 0.9% comments Poke 1 SLoC

Contains (autotools obfuscated code, 475KB) jpeg-9d/configure, (obscure autoconf code, 12KB) jpeg-9d/configure.ac, (obscure autoconf code, 1KB) zlib-1.2.11/contrib/minizip/configure.ac

qpdf-rs

github actions crates license license docs.rs

Overview

This project contains Rust safe bindings to a popular QPDF C++ library. It uses the QPDF C API exposed via qpdf-c.h header.

Tested on the following targets:

  • x86_64-unknown-linux-gnu
  • aarch64-unknown-linux-gnu
  • x86_64-pc-windows-gnu
  • x86_64-pc-windows-msvc
  • x86_64-apple-darwin
  • aarch64-apple-darwin

The prebuilt bindings for those targets are included in the source tree.

By default, pkg-config will be used to link against the system library libqpdf.

If the vendored feature is enabled, a vendored source tree of qpdf is built and linked statically.

The legacy feature enables bindings to the r2/3/4 encryption options which are available in qpdf 10.x but not 11.x.

Usage example

use qpdf::*;

fn make_pdf_from_scratch() -> qpdf::Result<Vec<u8>> {
    let qpdf = QPdf::empty();

    let font = qpdf
        .parse_object(
            r#"<<
                        /Type /Font
                        /Subtype /Type1
                        /Name /F1
                        /BaseFont /Helvetica
                        /Encoding /WinAnsiEncoding
                      >>"#,
        )?;

    let procset = qpdf.parse_object("[/PDF /Text]")?;
    let contents = qpdf.new_stream(b"BT /F1 15 Tf 72 720 Td (First Page) Tj ET\n");
    let mediabox = qpdf.parse_object("[0 0 612 792]")?;
    let rfont = qpdf.new_dictionary_from([("/F1", font.into_indirect())]);
    let resources = qpdf.new_dictionary_from([
        ("/ProcSet", procset.into_indirect()),
        ("/Font", rfont.into())
    ]);
    let page = qpdf.new_dictionary_from([
        ("/Type", qpdf.new_name("/Page")),
        ("/MediaBox", mediabox),
        ("/Contents", contents.into()),
        ("/Resources", resources.into()),
    ]);

    qpdf.add_page(&page.into_indirect(), true)?;

    let mem = qpdf
        .writer()
        .static_id(true)
        .force_pdf_version("1.7")
        .normalize_content(true)
        .preserve_unreferenced_objects(false)
        .object_stream_mode(ObjectStreamMode::Preserve)
        .compress_streams(false)
        .stream_data_mode(StreamDataMode::Preserve)
        .write_to_memory()?;

    Ok(mem)
}

Additional build requirements

  • C/C++ compiler
  • For the targets which do not have prebuilt bindgen bindings:
    • Installed clang/llvm (with libclang shared library) for bindgen build-time invocation
    • For cross-compilation a custom sysroot must be passed to clang via BINDGEN_EXTRA_CLANG_ARGS environment variable, for example: BINDGEN_EXTRA_CLANG_ARGS="--sysroot=/usr/x86_64-w64-mingw32/sys-root"

License

Licensed under Apache 2.0 license.

No runtime deps