| 1 |
#[macro_use] |
| 2 |
extern crate lopdf; |
| 3 |
use lopdf::content::{Content, Operation}; |
| 4 |
use lopdf::{Document, Object, Stream}; |
| 5 |
|
| 6 |
fn main() { |
| 7 |
// with_version specifes the PDF version this document complies with. |
| 8 |
let mut doc = Document::with_version("1.5"); |
| 9 |
|
| 10 |
// Object IDs are used for cross referencing in PDF documents. |
| 11 |
// `lopdf` helps keep track of them for us. They are simple integers. |
| 12 |
// Calls to `doc.new_object_id` and `doc.add_object` return an object IDs. |
| 13 |
|
| 14 |
// "Pages" is the root node of the page tree |
| 15 |
let pages_id = doc.new_object_id(); |
| 16 |
|
| 17 |
// fonts are dictionaries. The type, subtype and basefont tags |
| 18 |
// are straight out of the PDF reference manual |
| 19 |
// |
| 20 |
// The dictionary macro is a helper that allows complex |
| 21 |
// key, value relationships to be represented in a simpler |
| 22 |
// visual manner, similar to a match statement. |
| 23 |
// A dictionary is implemented as an IndexMap of Vec<u8>, and Object |
| 24 |
let font_id = doc.add_object(dictionary! { |
| 25 |
// type of dictionary |
| 26 |
"Type" => "Font", |
| 27 |
// type of font, type1 is simple postscript font |
| 28 |
"Subtype" => "Type1", |
| 29 |
// basefont is postscript name of font for type1 font. |
| 30 |
// See PDF reference document for more details |
| 31 |
"BaseFont" => "Courier", |
| 32 |
}); |
| 33 |
|
| 34 |
// Font dictionaries need to be added into resource dictionaries in order |
| 35 |
// to be used. Resource dictionaries can contain more than just fonts, |
| 36 |
// but normally just contains fonts. |
| 37 |
// Only one resource dictionary is allowed per page tree root |
| 38 |
let resources_id = doc.add_object(dictionary! { |
| 39 |
// Fonts are actually triplely nested dictionaries. Fun! |
| 40 |
"Font" => dictionary! { |
| 41 |
// F1 is the font name used when writing text. |
| 42 |
// It must be unique in the document. It does not |
| 43 |
// have to be F1 |
| 44 |
"F1" => font_id, |
| 45 |
}, |
| 46 |
}); |
| 47 |
|
| 48 |
// Content is a wrapper struct around an operations struct that contains a vector of operations |
| 49 |
// The operations struct contains a vector of operations that match up with a particular PDF |
| 50 |
// operator and operands. |
| 51 |
// Refer to the PDF spec for more details on these operators and operands. |
| 52 |
// Note, the operators and operands are specified in a reverse order than they |
| 53 |
// actually appear in the PDF file itself. |
| 54 |
let content = Content { |
| 55 |
operations: vec![ |
| 56 |
// BT begins a text element. it takes no operands |
| 57 |
Operation::new("BT", vec![]), |
| 58 |
// Tf specifies the font and font size. Font scaling is complicated in PDFs. |
| 59 |
// Refer to the PDF spec for more info. |
| 60 |
// The `into()` methods convert the types into |
| 61 |
// an enum that represents the basic object types in PDF documents. |
| 62 |
Operation::new("Tf", vec!["F1".into(), 48.into()]), |
| 63 |
// Td adjusts the translation components of the text matrix. When used for the first |
| 64 |
// time after BT, it sets the initial text position on the page. |
| 65 |
// Note: PDF documents have Y=0 at the bottom. Thus 600 to print text near the top. |
| 66 |
Operation::new("Td", vec![100.into(), 600.into()]), |
| 67 |
// Tj prints a string literal to the page. By default, this is black text that is |
| 68 |
// filled in. There are other operators that can produce various textual effects and |
| 69 |
// colors |
| 70 |
Operation::new("Tj", vec![Object::string_literal("Hello World!")]), |
| 71 |
// ET ends the text element |
| 72 |
Operation::new("ET", vec![]), |
| 73 |
], |
| 74 |
}; |
| 75 |
|
| 76 |
// Streams are a dictionary followed by a sequence of bytes. What that sequence of bytes |
| 77 |
// represents, depends on context. |
| 78 |
// The stream dictionary is set internally by lopdf and normally doesn't |
| 79 |
// need to be manually nanipulated. It contains keys such as |
| 80 |
// Length, Filter, DecodeParams, etc. |
| 81 |
let content_id = doc.add_object(Stream::new(dictionary! {}, content.encode().unwrap())); |
| 82 |
|
| 83 |
// Page is a dictionary that represents one page of a PDF file. |
| 84 |
// Its required fields are "Type", "Parent" and "Contents". |
| 85 |
let page_id = doc.add_object(dictionary! { |
| 86 |
"Type" => "Page", |
| 87 |
"Parent" => pages_id, |
| 88 |
"Contents" => content_id, |
| 89 |
}); |
| 90 |
|
| 91 |
// Again, pages is the root of the page tree. The ID was already created |
| 92 |
// at the top of the page, since we needed it to assign to the parent element of the page |
| 93 |
// dictionary |
| 94 |
// |
| 95 |
// These are just the basic requirements for a page tree root object. There are also many |
| 96 |
// additional entries that can be added to the dictionary if needed. Some of these can also be |
| 97 |
// defined on the page dictionary itself, and not inherited from the page tree root. |
| 98 |
let pages = dictionary! { |
| 99 |
// Type of dictionary |
| 100 |
"Type" => "Pages", |
| 101 |
// Vector of page IDs in document. Normally would contain more than one ID and be produced |
| 102 |
// using a loop of some kind |
| 103 |
"Kids" => vec![page_id.into()], |
| 104 |
// Page count |
| 105 |
"Count" => 1, |
| 106 |
// ID of resources dictionary, defined earlier |
| 107 |
"Resources" => resources_id, |
| 108 |
// a rectangle that defines the boundaries of the physical or digital media. This is the |
| 109 |
// "Page Size" |
| 110 |
"MediaBox" => vec![0.into(), 0.into(), 595.into(), 842.into()], |
| 111 |
}; |
| 112 |
|
| 113 |
// using insert() here, instead of add_object() since the id is already known. |
| 114 |
doc.objects.insert(pages_id, Object::Dictionary(pages)); |
| 115 |
|
| 116 |
// Creating document catalog. |
| 117 |
// There are many more entries allowed in the catalog dictionary. |
| 118 |
let catalog_id = doc.add_object(dictionary! { |
| 119 |
"Type" => "Catalog", |
| 120 |
"Pages" => pages_id, |
| 121 |
}); |
| 122 |
|
| 123 |
// Root key in trailer is set here to ID of document catalog, |
| 124 |
// remainder of trailer is set during doc.save(). |
| 125 |
doc.trailer.set("Root", catalog_id); |
| 126 |
doc.compress(); |
| 127 |
|
| 128 |
// Store file in current working directory. |
| 129 |
doc.save("example.pdf").unwrap(); |
| 130 |
} |