Skip to main content

pdfkit/
document.rs

1use std::path::Path;
2use std::ptr;
3
4use crate::document_delegate::PdfDocumentDelegateHandle;
5use crate::error::{PdfKitError, Result};
6use crate::ffi;
7use crate::handle::ObjectHandle;
8use crate::outline::PdfOutline;
9use crate::page::PdfPage;
10use crate::selection::PdfSelection;
11use crate::types::{
12    PdfDocumentAttributes, PdfDocumentInfo, PdfDocumentWriteOptions, PdfPoint,
13    PdfSelectionGranularity,
14};
15use crate::util::{c_string, parse_json, path_to_c_string, required_handle, take_string};
16
17/// Wraps `PDFDocument`.
18#[derive(Debug, Clone)]
19pub struct PdfDocument {
20    handle: ObjectHandle,
21}
22
23impl PdfDocument {
24    pub(crate) fn from_handle(handle: ObjectHandle) -> Self {
25        Self { handle }
26    }
27
28    /// Wraps `PDFDocument()`.
29    pub fn new() -> Result<Self> {
30        let mut out_document = ptr::null_mut();
31        let mut out_error = ptr::null_mut();
32        let status = unsafe { ffi::pdf_document_new(&mut out_document, &mut out_error) };
33        crate::util::status_result(status, out_error)?;
34        Ok(Self::from_handle(required_handle(
35            out_document,
36            "PDFDocument",
37        )?))
38    }
39
40    /// Wraps `PDFDocument(url:)`.
41    pub fn from_url(path: impl AsRef<Path>) -> Result<Self> {
42        let path = path_to_c_string(path.as_ref())?;
43        let mut out_document = ptr::null_mut();
44        let mut out_error = ptr::null_mut();
45        let status = unsafe {
46            ffi::pdf_document_new_with_url(path.as_ptr(), &mut out_document, &mut out_error)
47        };
48        crate::util::status_result(status, out_error)?;
49        Ok(Self::from_handle(required_handle(
50            out_document,
51            "PDFDocument",
52        )?))
53    }
54
55    /// Wraps `PDFDocument(data:)`.
56    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
57        let mut out_document = ptr::null_mut();
58        let mut out_error = ptr::null_mut();
59        let status = unsafe {
60            ffi::pdf_document_new_with_data(
61                bytes.as_ptr(),
62                bytes.len(),
63                &mut out_document,
64                &mut out_error,
65            )
66        };
67        crate::util::status_result(status, out_error)?;
68        Ok(Self::from_handle(required_handle(
69            out_document,
70            "PDFDocument",
71        )?))
72    }
73
74    /// Wraps the corresponding `PDFDocument` API.
75    pub fn info(&self) -> Result<PdfDocumentInfo> {
76        parse_json(
77            unsafe { ffi::pdf_document_info_json(self.handle.as_ptr()) },
78            "PDFDocument",
79        )
80    }
81
82    /// Wraps the corresponding `PDFDocument` API.
83    pub fn attributes(&self) -> Result<PdfDocumentAttributes> {
84        parse_json(
85            unsafe { ffi::pdf_document_attributes_json(self.handle.as_ptr()) },
86            "PDFDocument attributes",
87        )
88    }
89
90    /// Wraps the corresponding `PDFDocument` API.
91    #[must_use]
92    pub fn string(&self) -> Option<String> {
93        take_string(unsafe { ffi::pdf_document_string(self.handle.as_ptr()) })
94    }
95
96    /// Wraps the corresponding `PDFDocument` API.
97    #[must_use]
98    pub fn page_count(&self) -> usize {
99        unsafe { ffi::pdf_document_page_count(self.handle.as_ptr()) as usize }
100    }
101
102    /// Wraps the corresponding `PDFDocument` API.
103    #[must_use]
104    pub fn page(&self, index: usize) -> Option<PdfPage> {
105        let ptr = unsafe { ffi::pdf_document_page_at(self.handle.as_ptr(), index as u64) };
106        unsafe { ObjectHandle::from_retained_ptr(ptr) }.map(PdfPage::from_handle)
107    }
108
109    /// Wraps the corresponding `PDFDocument` API.
110    #[must_use]
111    pub fn pages(&self) -> Vec<PdfPage> {
112        (0..self.page_count())
113            .filter_map(|index| self.page(index))
114            .collect()
115    }
116
117    /// Wraps the corresponding `PDFDocument` API.
118    #[must_use]
119    pub fn page_index(&self, page: &PdfPage) -> Option<usize> {
120        let index =
121            unsafe { ffi::pdf_document_index_for_page(self.handle.as_ptr(), page.as_handle_ptr()) };
122        (index != u64::MAX).then_some(index as usize)
123    }
124
125    /// Wraps the corresponding `PDFDocument` API.
126    #[must_use]
127    pub fn outline_root(&self) -> Option<PdfOutline> {
128        let ptr = unsafe { ffi::pdf_document_outline_root(self.handle.as_ptr()) };
129        unsafe { ObjectHandle::from_retained_ptr(ptr) }.map(PdfOutline::from_handle)
130    }
131
132    /// Wraps the corresponding `PDFDocument` API.
133    pub fn set_outline_root(&self, outline: Option<&PdfOutline>) -> Result<()> {
134        let mut out_error = ptr::null_mut();
135        let status = unsafe {
136            ffi::pdf_document_set_outline_root(
137                self.handle.as_ptr(),
138                outline.map_or(ptr::null_mut(), PdfOutline::as_handle_ptr),
139                &mut out_error,
140            )
141        };
142        crate::util::status_result(status, out_error)
143    }
144
145    /// Wraps the corresponding `PDFDocument` API.
146    #[must_use]
147    pub fn outline_item_for_selection(&self, selection: &PdfSelection) -> Option<PdfOutline> {
148        let ptr = unsafe {
149            ffi::pdf_document_outline_item_for_selection(
150                self.handle.as_ptr(),
151                selection.as_handle_ptr(),
152            )
153        };
154        unsafe { ObjectHandle::from_retained_ptr(ptr) }.map(PdfOutline::from_handle)
155    }
156
157    /// Wraps the corresponding `PDFDocument` API.
158    #[must_use]
159    pub fn selection_for_entire_document(&self) -> Option<PdfSelection> {
160        let ptr = unsafe { ffi::pdf_document_selection_for_entire_document(self.handle.as_ptr()) };
161        unsafe { ObjectHandle::from_retained_ptr(ptr) }.map(PdfSelection::from_handle)
162    }
163
164    /// Wraps the corresponding `PDFDocument` API.
165    #[must_use]
166    pub fn selection_from_page_points(
167        &self,
168        start_page: &PdfPage,
169        start_point: PdfPoint,
170        end_page: &PdfPage,
171        end_point: PdfPoint,
172    ) -> Option<PdfSelection> {
173        let ptr = unsafe {
174            ffi::pdf_document_selection_from_pages_points(
175                self.handle.as_ptr(),
176                start_page.as_handle_ptr(),
177                start_point.x,
178                start_point.y,
179                end_page.as_handle_ptr(),
180                end_point.x,
181                end_point.y,
182            )
183        };
184        unsafe { ObjectHandle::from_retained_ptr(ptr) }.map(PdfSelection::from_handle)
185    }
186
187    /// Wraps the corresponding `PDFDocument` API.
188    #[must_use]
189    pub fn selection_from_page_points_with_granularity(
190        &self,
191        start_page: &PdfPage,
192        start_point: PdfPoint,
193        end_page: &PdfPage,
194        end_point: PdfPoint,
195        granularity: PdfSelectionGranularity,
196    ) -> Option<PdfSelection> {
197        let ptr = unsafe {
198            ffi::pdf_document_selection_from_pages_points_with_granularity(
199                self.handle.as_ptr(),
200                start_page.as_handle_ptr(),
201                start_point.x,
202                start_point.y,
203                end_page.as_handle_ptr(),
204                end_point.x,
205                end_point.y,
206                granularity.as_raw(),
207            )
208        };
209        unsafe { ObjectHandle::from_retained_ptr(ptr) }.map(PdfSelection::from_handle)
210    }
211
212    /// Wraps the corresponding `PDFDocument` API.
213    #[must_use]
214    pub fn selection_from_page_characters(
215        &self,
216        start_page: &PdfPage,
217        start_character: usize,
218        end_page: &PdfPage,
219        end_character: usize,
220    ) -> Option<PdfSelection> {
221        let ptr = unsafe {
222            ffi::pdf_document_selection_from_pages_characters(
223                self.handle.as_ptr(),
224                start_page.as_handle_ptr(),
225                start_character as u64,
226                end_page.as_handle_ptr(),
227                end_character as u64,
228            )
229        };
230        unsafe { ObjectHandle::from_retained_ptr(ptr) }.map(PdfSelection::from_handle)
231    }
232
233    /// Wraps the corresponding `PDFDocument` API.
234    pub fn unlock(&self, password: &str) -> Result<bool> {
235        let password = c_string(password)?;
236        Ok(unsafe { ffi::pdf_document_unlock(self.handle.as_ptr(), password.as_ptr()) != 0 })
237    }
238
239    /// Wraps the corresponding `PDFDocument` API.
240    pub fn set_delegate(&self, delegate: Option<&PdfDocumentDelegateHandle>) -> Result<()> {
241        let mut out_error = ptr::null_mut();
242        let status = unsafe {
243            ffi::pdf_document_set_delegate(
244                self.handle.as_ptr(),
245                delegate.map_or(ptr::null_mut(), PdfDocumentDelegateHandle::as_handle_ptr),
246                &mut out_error,
247            )
248        };
249        crate::util::status_result(status, out_error)
250    }
251
252    /// Wraps the corresponding `PDFDocument` API.
253    pub fn write_to_url(&self, path: impl AsRef<Path>) -> Result<()> {
254        let path = path_to_c_string(path.as_ref())?;
255        let mut out_error = ptr::null_mut();
256        let status = unsafe {
257            ffi::pdf_document_write_to_url(self.handle.as_ptr(), path.as_ptr(), &mut out_error)
258        };
259        crate::util::status_result(status, out_error)
260    }
261
262    /// Wraps the corresponding `PDFDocument` API.
263    pub fn write_to_url_with_options(
264        &self,
265        path: impl AsRef<Path>,
266        options: &PdfDocumentWriteOptions,
267    ) -> Result<()> {
268        let path = path_to_c_string(path.as_ref())?;
269        let options_json = serde_json::to_string(options).map_err(|error| {
270            PdfKitError::new(
271                ffi::status::FRAMEWORK,
272                format!("failed to encode PDFDocument write options: {error}"),
273            )
274        })?;
275        let options_json = c_string(&options_json)?;
276        let mut out_error = ptr::null_mut();
277        let status = unsafe {
278            ffi::pdf_document_write_to_url_with_options(
279                self.handle.as_ptr(),
280                path.as_ptr(),
281                options_json.as_ptr(),
282                &mut out_error,
283            )
284        };
285        crate::util::status_result(status, out_error)
286    }
287
288    /// Wraps the corresponding `PDFDocument` API.
289    pub fn insert_page(&self, page: &PdfPage, index: usize) -> Result<()> {
290        let mut out_error = ptr::null_mut();
291        let status = unsafe {
292            ffi::pdf_document_insert_page(
293                self.handle.as_ptr(),
294                page.as_handle_ptr(),
295                index as u64,
296                &mut out_error,
297            )
298        };
299        crate::util::status_result(status, out_error)
300    }
301
302    /// Wraps the corresponding `PDFDocument` API.
303    pub fn remove_page(&self, index: usize) -> Result<()> {
304        let mut out_error = ptr::null_mut();
305        let status = unsafe {
306            ffi::pdf_document_remove_page_at(self.handle.as_ptr(), index as u64, &mut out_error)
307        };
308        crate::util::status_result(status, out_error)
309    }
310
311    /// Wraps the corresponding `PDFDocument` API.
312    pub fn exchange_pages(&self, index_a: usize, index_b: usize) -> Result<()> {
313        let mut out_error = ptr::null_mut();
314        let status = unsafe {
315            ffi::pdf_document_exchange_pages(
316                self.handle.as_ptr(),
317                index_a as u64,
318                index_b as u64,
319                &mut out_error,
320            )
321        };
322        crate::util::status_result(status, out_error)
323    }
324
325    pub(crate) fn as_handle_ptr(&self) -> *mut core::ffi::c_void {
326        self.handle.as_ptr()
327    }
328}