1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
//! Defines the [PdfDocument] struct, the entry point to all Pdfium functionality
//! related to a single PDF file.
use crate::bindgen::FPDF_DOCUMENT;
use crate::bindings::PdfiumLibraryBindings;
use crate::bookmarks::PdfBookmarks;
use crate::error::PdfiumError;
use crate::form::PdfForm;
use crate::metadata::PdfMetadata;
use crate::pages::PdfPages;
use crate::permissions::PdfPermissions;
use crate::utils::files::FpdfFileAccessExt;
use std::os::raw::c_int;
#[cfg(not(target_arch = "wasm32"))]
use crate::utils::files::get_pdfium_file_writer_from_writer;
#[cfg(not(target_arch = "wasm32"))]
use crate::error::PdfiumInternalError;
#[cfg(not(target_arch = "wasm32"))]
use std::io::Write;
/// The file version of a [PdfDocument].
///
/// A list of PDF file versions is available at <https://en.wikipedia.org/wiki/History_of_PDF>.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum PdfDocumentVersion {
/// No version information is available. This is the case if the [PdfDocument]
/// was created via a call to `Pdfium::create_new_pdf()` rather than loaded from a file.
Unset,
/// PDF 1.0, first published in 1993, supported by Acrobat Reader Carousel (1.0) onwards.
Pdf1_0,
/// PDF 1.1, first published in 1994, supported by Acrobat Reader 2.0 onwards.
Pdf1_1,
/// PDF 1.2, first published in 1996, supported by Acrobat Reader 3.0 onwards.
Pdf1_2,
/// PDF 1.3, first published in 2000, supported by Acrobat Reader 4.0 onwards.
Pdf1_3,
/// PDF 1.4, first published in 2001, supported by Acrobat Reader 5.0 onwards.
Pdf1_4,
/// PDF 1.5, first published in 2003, supported by Acrobat Reader 6.0 onwards.
Pdf1_5,
/// PDF 1.6, first published in 2004, supported by Acrobat Reader 7.0 onwards.
Pdf1_6,
/// PDF 1.7, first published in 2006, supported by Acrobat Reader 8.0 onwards,
/// adopted as ISO open standard 32000-1 in 2008. Certain proprietary Adobe
/// extensions to PDF 1.7 are only fully supported in Acrobat Reader X (10.0)
/// and later.
Pdf1_7,
/// PDF 2.0, first published in 2017, ISO open standard 32000-2.
Pdf2_0,
/// A two-digit raw file version number. For instance, a value of 21 would indicate
/// PDF version 2.1, a value of 34 would indicate PDF version 3.4, and so on.
/// Only used when the file version number is not directly recognized by
/// pdfium-render.
Other(i32),
}
impl PdfDocumentVersion {
/// The default [PdfDocumentVersion] applied to new documents.
pub const DEFAULT_VERSION: PdfDocumentVersion = PdfDocumentVersion::Pdf1_7;
#[inline]
pub(crate) fn from_pdfium(version: i32) -> Self {
match version {
10 => PdfDocumentVersion::Pdf1_0,
11 => PdfDocumentVersion::Pdf1_1,
12 => PdfDocumentVersion::Pdf1_2,
13 => PdfDocumentVersion::Pdf1_3,
14 => PdfDocumentVersion::Pdf1_4,
15 => PdfDocumentVersion::Pdf1_5,
16 => PdfDocumentVersion::Pdf1_6,
17 => PdfDocumentVersion::Pdf1_7,
20 => PdfDocumentVersion::Pdf2_0,
_ => PdfDocumentVersion::Other(version),
}
}
#[inline]
pub(crate) fn as_pdfium(&self) -> Option<i32> {
match self {
PdfDocumentVersion::Pdf1_0 => Some(10),
PdfDocumentVersion::Pdf1_1 => Some(11),
PdfDocumentVersion::Pdf1_2 => Some(12),
PdfDocumentVersion::Pdf1_3 => Some(13),
PdfDocumentVersion::Pdf1_4 => Some(14),
PdfDocumentVersion::Pdf1_5 => Some(15),
PdfDocumentVersion::Pdf1_6 => Some(16),
PdfDocumentVersion::Pdf1_7 => Some(17),
PdfDocumentVersion::Pdf2_0 => Some(20),
PdfDocumentVersion::Other(value) => Some(*value),
PdfDocumentVersion::Unset => None,
}
}
}
/// An entry point to all the various object collections contained in a single PDF file.
/// These collections include:
/// * [PdfDocument::pages()], all the [PdfPages] in the document.
/// * [PdfDocument::metadata()], all the [PdfMetadata] tags in the document.
/// * [PdfDocument::form()], the [PdfForm] optionally embedded in the document.
/// * [PdfDocument::bookmarks()], all the [PdfBookmarks] in the document.
/// * [PdfDocument::permissions()], settings relating to security handlers and document permissions
/// for the document.
pub struct PdfDocument<'a> {
handle: FPDF_DOCUMENT,
form: Option<PdfForm<'a>>,
bindings: &'a dyn PdfiumLibraryBindings,
output_version: Option<PdfDocumentVersion>,
file_access_reader: Option<Box<FpdfFileAccessExt>>,
}
impl<'a> PdfDocument<'a> {
#[inline]
pub(crate) fn from_pdfium(
handle: FPDF_DOCUMENT,
bindings: &'a dyn PdfiumLibraryBindings,
) -> Self {
Self {
handle,
form: PdfForm::from_pdfium(handle, bindings),
bindings,
output_version: None,
file_access_reader: None,
}
}
/// Returns the internal FPDF_DOCUMENT handle for this [PdfDocument].
#[inline]
pub(crate) fn get_handle(&self) -> &FPDF_DOCUMENT {
&self.handle
}
/// Returns the [PdfiumLibraryBindings] used by this [PdfDocument].
#[inline]
pub(crate) fn get_bindings(&self) -> &'a dyn PdfiumLibraryBindings {
self.bindings
}
/// Binds an `FPDF_FILEACCESS` reader to the lifetime of this [PdfDocument], so that
/// it will always be available for Pdfium to read data from as needed.
#[inline]
pub(crate) fn set_file_access_reader(&mut self, reader: Box<FpdfFileAccessExt>) {
self.file_access_reader = Some(reader);
}
/// Returns the file version of this [PdfDocument].
pub fn version(&self) -> PdfDocumentVersion {
let mut version: c_int = 0;
if self.bindings.FPDF_GetFileVersion(self.handle, &mut version) != 0 {
PdfDocumentVersion::from_pdfium(version)
} else {
PdfDocumentVersion::Unset
}
}
/// Sets the file version that will be used the next time this [PdfDocument] is saved
/// using the [PdfDocument::save_to_writer()] function.
pub fn set_version(&mut self, version: PdfDocumentVersion) {
self.output_version = Some(version);
}
/// Returns the collection of [PdfPages] in this [PdfDocument].
#[inline]
pub fn pages(&self) -> PdfPages {
PdfPages::new(self, self.bindings)
}
/// Returns the collection of [PdfMetadata] tags in this [PdfDocument].
#[inline]
pub fn metadata(&self) -> PdfMetadata {
PdfMetadata::new(self, self.bindings)
}
/// Returns a reference to the [PdfForm] embedded in this [PdfDocument], if any.
#[inline]
pub fn form(&self) -> Option<&PdfForm> {
self.form.as_ref()
}
/// Returns the collection of [PdfBookmarks] in this [PdfDocument].
#[inline]
pub fn bookmarks(&self) -> PdfBookmarks {
PdfBookmarks::new(self, self.bindings)
}
/// Returns the collection of [PdfPermissions] for this [PdfDocument].
#[inline]
pub fn permissions(&self) -> PdfPermissions {
PdfPermissions::new(self)
}
/// Copies all pages in the given [PdfDocument] into this [PdfDocument], appending them
/// to the end of this document's [PdfPages] collection.
///
/// For finer control over which pages are imported, and where they should be inserted,
/// use one of the [PdfPages::copy_page_from_document()], [PdfPages::copy_pages_from_document()],
/// or [PdfPages::copy_page_range_from_document()] functions.
///
/// Calling this function is equivalent to
///
/// ```
/// self.pages().copy_page_range_from_document(
/// document, // Source
/// document.pages().as_range_inclusive(), // Select all pages
/// self.pages().len() // Append to end of current document
/// );
/// ```
pub fn append(&mut self, document: &PdfDocument) -> Result<(), PdfiumError> {
self.pages().copy_page_range_from_document(
document,
document.pages().as_range_inclusive(),
self.pages().len(),
)
}
/// Writes this [PdfDocument] to the given writer.
///
/// This function is not available when compiling to WASM.
#[cfg(not(target_arch = "wasm32"))]
pub fn save_to_writer<W: Write>(&self, writer: W) -> Result<(), PdfiumError> {
// TODO: AJRC - 25/5/22 - investigate supporting the FPDF_INCREMENTAL, FPDF_NO_INCREMENTAL,
// and FPDF_REMOVE_SECURITY flags defined in fpdf_save.h. There's not a lot of information
// on what they actually do, however.
// Some small info at https://forum.patagames.com/posts/t155-PDF-SaveFlags.
let flags = 0;
let mut pdfium_file_writer = get_pdfium_file_writer_from_writer(writer);
let result = match self.output_version {
Some(version) => self.bindings.FPDF_SaveWithVersion(
self.handle,
pdfium_file_writer.as_fpdf_file_write_mut_ptr(),
flags,
version
.as_pdfium()
.unwrap_or_else(|| PdfDocumentVersion::DEFAULT_VERSION.as_pdfium().unwrap()),
),
None => self.bindings.FPDF_SaveAsCopy(
self.handle,
pdfium_file_writer.as_fpdf_file_write_mut_ptr(),
flags,
),
};
match self.bindings.is_true(result) {
true => {
// Pdfium's return value indicated success. Flush the buffer,
// returning any final I/O error.
pdfium_file_writer.flush().map_err(PdfiumError::IoError)
}
false => {
// Pdfium's return value indicated failure.
Err(PdfiumError::PdfiumLibraryInternalError(
self.bindings
.get_pdfium_last_error()
.unwrap_or(PdfiumInternalError::Unknown),
))
}
}
}
}
impl<'a> Drop for PdfDocument<'a> {
/// Closes this [PdfDocument], releasing held memory and, if the document was loaded
/// from a file, the file handle on the document.
#[inline]
fn drop(&mut self) {
self.bindings.FPDF_CloseDocument(self.handle);
}
}