2 releases

0.1.1 Nov 23, 2020
0.1.0 Nov 23, 2020

#1404 in Text processing

MPL-2.0 license

65KB
1.5K SLoC

pdf-create

Crates.io Docs

This is yet another PDF creation library, developed as part of the Signum!-Document-Toolbox. In some respect, it's a prototype for missing parts of the pdf crate.

Rationale

The main goal of this crate is the ability to use custom Type3 fonts to create a document. The main design philosophy is the idea of storing all data as rustic types and serializing them through a trait similar to the std::fmt API.

As a PDF is fundamentally a list of objects that are linked using their IDs and serializing some struct requires the referenced IDs to be known, this crate has a high-level and a low-level component that represent the document before and after assigning global IDs.

Features

  • Arbitrary content & glyph streams
  • Custom /Info values
  • Type3 fonts
  • Outlines
  • Page Labels
  • Implementations of Ascii85Decode encoding and PDFDocEncoding

Basic Usage

This example creates a single empty A4 page with no text:

use chrono::Local;
use pdf_create::{
    common::Point,
    common::{PdfString, Rectangle},
    high::{Handle, Page, Resources},
};

// Create a new handle
let mut doc = Handle::new();

// Set some metadata
doc.info.author = Some(PdfString::new("Xiphoseer"));
doc.info.creator = Some(PdfString::new("SIGNUM (c) 1986-93 F. Schmerbeck"));
doc.info.producer = Some(PdfString::new("Signum! Document Toolbox"));
doc.info.title = Some(PdfString::new("EMPTY.SDO"));
doc.info.mod_date = Some(Local::now());
doc.info.creation_date = Some(Local::now());

// Create a page
let page = Page {
    media_box: Rectangle {
        ll: Point { x: 0, y: 0 },
        ur: Point { x: 592, y: 842 },
    },
    resources: Resources::default(),
    contents: Vec::new(),
};

// Add the page to the document
doc.pages.push(page);

// Write the PDF to the console
let stdout = std::io::stdout();
let mut stdolock = stdout.lock();
doc.write(&mut stdolock)?;

Alternatives

  • If you are looking for a crate that can generate valid PDF files with arbitrary content, you should probably use lopdf
  • If you are looking for a crate to save a combination of graphics and text as a PDF, you should probably use printpdf, genpdf or pdf-canvas
  • If you are looking for a crate that can load and render a PDF, you should probably use pdf

Dependencies

~1MB
~18K SLoC