pub struct Writer { /* private fields */ }Expand description
Accumulates numbered objects and serializes them into a complete PDF
file. Objects are numbered in the order they are first claimed
(put, put_stream, or reserve), starting at 1, generation 0.
Implementations§
Source§impl Writer
impl Writer
Sourcepub fn new(options: WriteOptions) -> Writer
pub fn new(options: WriteOptions) -> Writer
Creates a writer with the given options.
Sourcepub fn new_encrypted(
options: WriteOptions,
encryptor: Encryptor,
encrypt_dict: Dict,
) -> Writer
pub fn new_encrypted( options: WriteOptions, encryptor: Encryptor, encrypt_dict: Dict, ) -> Writer
Creates a writer that encrypts every object it emits under
encryptor, except the /Encrypt dictionary itself, the /ID
trailer strings, and the /Type /XRef cross-reference stream
(ISO 32000-2 §7.6.2, which never encrypts the handshake objects a
reader needs before it has a file key). encrypt_dict is the
complete /Encrypt dictionary to place in the trailer; both
values come from Encryptor::aes256 or
Encryptor::aes256_with_rng. finish reserves one extra object
number for encrypt_dict and adds the trailer’s /Encrypt entry;
WriteOptions itself carries no encryption state.
Sourcepub fn reserve(&mut self) -> ObjRef
pub fn reserve(&mut self) -> ObjRef
Claims an object number now to be filled later — for cycles like the page tree, where children point at a parent not yet built.
Sourcepub fn put_stream(&mut self, dict: Dict, data: Vec<u8>) -> ObjRef
pub fn put_stream(&mut self, dict: Dict, data: Vec<u8>) -> ObjRef
Adds a stream object. /Length is computed on emission; when
WriteOptions::compress is set and dict names no /Filter,
the data is Flate-compressed and /Filter /FlateDecode added.
Sourcepub fn put_stream_raw(&mut self, dict: Dict, data: Vec<u8>) -> ObjRef
pub fn put_stream_raw(&mut self, dict: Dict, data: Vec<u8>) -> ObjRef
Adds a stream object without touching its filters — for data that
is already encoded (e.g. a JPEG passed through as /DCTDecode,
with /Filter set by the caller). /Length is still computed.
Sourcepub fn fill(&mut self, r: ObjRef, obj: Object) -> Result<()>
pub fn fill(&mut self, r: ObjRef, obj: Object) -> Result<()>
Fills a previously reserved object.
Sourcepub fn set_info(&mut self, info: ObjRef)
pub fn set_info(&mut self, info: ObjRef)
Registers the document information dictionary for the trailer.
Sourcepub fn compress(&self) -> bool
pub fn compress(&self) -> bool
The WriteOptions::compress value this writer was created with.
Sourcepub fn finish(self, root: ObjRef) -> Result<Vec<u8>>
pub fn finish(self, root: ObjRef) -> Result<Vec<u8>>
Serializes everything into a complete PDF file: header with binary
comment, all objects (packed into object streams where options
allow), the cross-reference, and the trailer with root, the
registered info dictionary, and a /ID pair derived from a
SHA-256 of the emitted body.
Sourcepub fn finish_into(self, root: ObjRef, out: impl Write) -> Result<()>
pub fn finish_into(self, root: ObjRef, out: impl Write) -> Result<()>
Writer::finish streaming into a std::io::Write: the same
bytes, delivered in bounded chunks, so the whole file never sits in
one buffer. Unlike finish, an error can leave a prefix of the
file already written to out. No flush is performed.
Sourcepub async fn finish_into_with<S: AsyncByteSink>(
self,
root: ObjRef,
sink: S,
) -> Result<S>
pub async fn finish_into_with<S: AsyncByteSink>( self, root: ObjRef, sink: S, ) -> Result<S>
Writer::finish streaming into any AsyncByteSink — the
asynchronous twin of Writer::finish_into, and the one emission
implementation all three finishes drive. Bytes arrive in bounded
chunks (per header, object and cross-reference section; a stream’s
data is its own chunk). An error can leave a prefix of the file
already written. Hands the sink back unflushed.