Skip to main content

Paper

Struct Paper 

Source
pub struct Paper { /* private fields */ }
Expand description

A loaded PDF, Word document, Excel workbook, EPUB book or web page, ready to be analysed and rendered.

Typical use: Paper::from_bytes, Paper::analyze, then Paper::to_html once per output mode (the analysis does not depend on the mode, so it is computed once).

Implementations§

Source§

impl Paper

Source

pub fn from_bytes(data: Vec<u8>) -> Result<Self>

Loads a PDF, a Word document (.docx, .doc), an Excel workbook (.xlsx), an EPUB book (.epub) or a web page (HTML) from its bytes; the format is recognised from the bytes. The pictures of a web page loaded so are only those inside it (data: URLs): Paper::from_path reads them next to its file, Paper::from_url fetches them.

§Errors

When the data is not a readable PDF, Word document, workbook, EPUB book or web page, or the PDF has no pages.

Source

pub fn from_bytes_with(data: Vec<u8>, excel: &ExcelConfig) -> Result<Self>

Loads a file like Paper::from_bytes, reading a workbook with the thresholds excel (they do not apply to the other formats).

§Errors

As Paper::from_bytes.

Source

pub fn from_path(path: impl AsRef<Path>) -> Result<Self>

Loads the file at path, as Paper::from_bytes does; the pictures and style sheets of a web page are read next to it.

§Errors

When the file cannot be read, or Paper::from_bytes fails.

Source

pub fn from_path_with( path: impl AsRef<Path>, excel: &ExcelConfig, ) -> Result<Self>

Loads the file at path like Paper::from_path, reading a workbook with the thresholds excel.

§Errors

As Paper::from_path.

Source

pub fn from_url(url: &str) -> Result<Self>

Fetches the document at the http(s) address url and loads it, as Paper::from_bytes does: a web page with its pictures (fetched too), or a PDF, a Word document or an EPUB book. Its source (Meta::source) is the address.

§Errors

When the document cannot be fetched (see source::web::fetch) or read.

Source

pub fn from_page_at(data: &[u8], url: &str) -> Result<Self>

Loads the web page data saved from the http(s) address url: its links and pictures are read as at that address, and its pictures are fetched. Its source (Meta::source) is the address.

§Errors

When url is not a URL.

Source

pub fn format(&self) -> InputFormat

The format of the file.

Source

pub fn to_pdf(&self) -> Result<Vec<u8>>

The document laid out in pages, as a PDF: a Word document as Word lays it out; a workbook printed as Excel prints it (each sheet’s print area cut into pages by its print settings, with the look of every cell), as WorkbookPdfConfig::default does; with WorkbookPdfStyle::Document, laid out as a document instead; an EPUB book or a web page written as a Word document first, which is laid out. A PDF already has its pages.

§Errors

When the paper is a PDF, no font is installed nor given with crate::add_font, or the layout cannot be written.

Source

pub fn to_pdf_with(&self, config: &WorkbookPdfConfig) -> Result<Vec<u8>>

The document laid out in pages, as Paper::to_pdf does; a workbook with the page, fonts and table limits of config (they do not apply to a Word document).

§Errors

As Paper::to_pdf.

Source

pub fn with_source(self, source: impl Into<String>) -> Self

Records where the PDF came from (file name, arXiv id, …); it ends up in Meta::source.

Source

pub fn with_png_compression(self, compression: PngCompression) -> Self

Sets how hard the PNG pictures of every output are compressed (PngCompression::Balanced by default). The pixels stay the same; PngCompression::Fast draws the pictures faster for larger files. Pictures with many colours are stored as JPEG when that is much smaller than their PNG, so a few more of them may be with Fast.

Source

pub fn page_count(&self) -> usize

Number of pages; 0 for a Word document, a workbook or a book, which have none.

Source

pub fn analyze(&self) -> Analysis

Runs content extraction and layout analysis with default options.

Source

pub fn analyze_with(&self, opts: AnalyzeOptions) -> Analysis

Runs content extraction and layout analysis.

Pages are read, and their lines built, on opts.jobs threads; the result does not depend on the number of threads. A Word document, a workbook or a book needs no analysis: its structure, read when it was loaded, is returned (opts does not apply to it).

Source

pub fn renderer(&self) -> Option<PageRenderer<'_>>

A renderer for pictures of pages and page regions, or None for a Word document or a workbook, which have no pages. Not thread-safe: every thread that draws needs its own.

Source

pub fn to_html(&self, analysis: &Analysis, opts: &HtmlOptions) -> HtmlOutput

Renders an analysis to HTML in the requested mode. analysis must come from this paper, since images are drawn from its PDF. A Word document is first laid out as Word lays it out: hard and hard-95 show its pages, medium its formatting in one continuous page (neither shows text edited with Paper::apply_content); when the layout fails, they give the friendly page. A workbook shows its sheets as Excel does, a tab per sheet, in every mode but friendly; a deck its slides drawn as pages, as PowerPoint draws them, in hard, hard-95 and medium (nor do they show edited text).

Source

pub fn write_html( &self, analysis: &Analysis, opts: &HtmlOptions, out: &mut dyn Write, file: &mut dyn FnMut(&str, &[u8]) -> Result<()>, ) -> Result<()>

Writes an analysis as HTML to out, like Paper::to_html, but without holding the whole page in memory: the pictures are drawn a few at a time and written as soon as they are ready, which matters for long documents (all the page pictures of hard mode). When pictures are not inlined, each picture file is given to file as (path relative to the HTML file, contents) instead of being returned. The bytes written are those of HtmlOutput::html. A conversion stopped by a CancelToken leaves out and the files given so far partly written.

§Errors

The first error of out or file.

Source

pub fn to_docx(&self, analysis: &Analysis, opts: &DocxOptions) -> Vec<u8> ⓘ

Writes an analysis as a Word document (.docx) and returns the bytes of the file. analysis must come from this paper, since the pictures are drawn from its PDF.

Source

pub fn to_docx_with_images( &self, analysis: &Analysis, opts: &DocxOptions, image_dir: &str, ) -> DocxOutput

Writes an analysis as a Word document, like Paper::to_docx, and also returns each picture of the document as a file of its own in image_dir (a relative path, such as images): the pictures stay embedded in the document too.

Source

pub fn to_markdown( &self, analysis: &Analysis, opts: &MarkdownOptions, ) -> MarkdownOutput

Writes an analysis as Markdown. analysis must come from this paper, since the pictures are drawn from its PDF.

Source

pub fn to_latex(&self, analysis: &Analysis, opts: &LatexOptions) -> LatexOutput

Writes an analysis as a LaTeX document (.tex) with its picture files. analysis must come from this paper, since the pictures are drawn from its PDF.

Source

pub fn to_typst(&self, analysis: &Analysis, opts: &TypstOptions) -> TypstOutput

Writes an analysis as a Typst document (.typ) with its picture files, in the layout of an article or of a printed book. analysis must come from this paper, since the pictures are drawn from its PDF.

Source

pub fn to_epub(&self, analysis: &Analysis, opts: &EpubOptions) -> Vec<u8> ⓘ

Writes an analysis as an EPUB book (.epub) and returns the bytes of the file. analysis must come from this paper, since the pictures are drawn from its PDF.

Source

pub fn to_json(&self, analysis: &Analysis, opts: &JsonOptions) -> JsonOutput

Writes an analysis as JSON: the content of the document, block by block, with its text in a form that can be edited and applied back with Paper::apply_content. analysis must come from this paper, since the pictures are drawn from its PDF.

Source

pub fn to_chunks(&self, analysis: &Analysis, opts: &ChunkOptions) -> Vec<Chunk>

Cuts an analysis into chunks for retrieval (RAG): pieces of at most opts.max_tokens tokens that follow the sections of the document, each headed by its section path, with the blocks and pages it comes from. Write them as JSON Lines with chunks_jsonl.

Source

pub fn apply_content( &self, analysis: &mut Analysis, json: &str, ) -> Result<ContentReport>

Applies an edited JSON file (written by Paper::to_json for this same PDF) to analysis: the edited text replaces the text of its blocks, and every output written from analysis afterwards shows it. Text that was not edited is left as it is. The hard mode, whose text layer follows the lines of the PDF, has no text for the edited blocks.

§Errors

When json is not a kakemono JSON file, was made from another PDF, or names blocks this analysis does not have (it was made with other analysis options or another version of kakemono). Problems with the text of single blocks are not errors: those blocks keep their text, and ContentReport::warnings says why.

Source

pub fn apply_content_in( &self, analysis: &mut Analysis, json: &str, base: &Path, ) -> Result<ContentReport>

Paper::apply_content for a JSON file in the folder base: a figure’s replace_image that is a relative path is read from there.

§Errors

As Paper::apply_content.

Auto Trait Implementations§

§

impl !Freeze for Paper

§

impl !RefUnwindSafe for Paper

§

impl !UnwindSafe for Paper

§

impl Forget for Paper

§

impl Move for Paper

§

impl Send for Paper
where Arc<XRef>: Send, RawVecInner: Send, Arc<Repr<'static>>: Send, OnceLock<Option<Vec<u8>>>: Send, Arc<ReaderContextData<'static>>: Send, &'static XRef: Send, Arc<dyn AsRef<[u8]> + Sync + Send>: Send, BoxRaw<Document>: Send, OnceLock<Option<Laid>>: Send,

§

impl Sync for Paper
where Arc<XRef>: Sync, RawVecInner: Sync, Arc<Repr<'static>>: Sync, OnceLock<Option<Vec<u8>>>: Sync, Arc<ReaderContextData<'static>>: Sync, Arc<SomeRepr>: Sync, Arc<dyn AsRef<[u8]> + Sync + Send>: Sync, BoxRaw<Document>: Sync, OnceLock<Option<Laid>>: Sync,

§

impl Unpin for Paper
where Arc<XRef>: Unpin, *const u8 is !null: Unpin, Arc<Repr<'static>>: Unpin, Align4<u32>: Unpin, Arc<ReaderContextData<'static>>: Unpin, &'static XRef: Unpin, Arc<dyn AsRef<[u8]> + Sync + Send>: Unpin, Box<Document>: Unpin, Box<MathNode>: Unpin, Arc<str>: Unpin, &'static str: Unpin,

§

impl UnsafeUnpin for Paper

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> Finish for T

Source§

fn finish(self)

Does nothing but move self, equivalent to drop.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(_simd: S, value: T) -> T

Source§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(_simd: S, value: T) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

Source§

impl<U, T> ToOwnedObj<U> for T
where U: FromObjRef<T>,

Source§

fn to_owned_obj(&self, data: FontData<'_>) -> U

Convert this type into T, using the provided data to resolve any offsets.
Source§

impl<U, T> ToOwnedTable<U> for T
where U: FromTableRef<T>,

Source§

fn to_owned_table(&self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V