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
impl Paper
Sourcepub fn from_bytes(data: Vec<u8>) -> Result<Self>
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.
Sourcepub fn from_bytes_with(data: Vec<u8>, excel: &ExcelConfig) -> Result<Self>
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
Sourcepub fn from_path(path: impl AsRef<Path>) -> Result<Self>
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.
Sourcepub fn from_path_with(
path: impl AsRef<Path>,
excel: &ExcelConfig,
) -> Result<Self>
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.
Sourcepub fn from_url(url: &str) -> Result<Self>
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.
Sourcepub fn from_page_at(data: &[u8], url: &str) -> Result<Self>
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.
Sourcepub fn format(&self) -> InputFormat
pub fn format(&self) -> InputFormat
The format of the file.
Sourcepub fn to_pdf(&self) -> Result<Vec<u8>>
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.
Sourcepub fn to_pdf_with(&self, config: &WorkbookPdfConfig) -> Result<Vec<u8>>
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.
Sourcepub fn with_source(self, source: impl Into<String>) -> Self
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.
Sourcepub fn with_png_compression(self, compression: PngCompression) -> Self
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.
Sourcepub fn page_count(&self) -> usize
pub fn page_count(&self) -> usize
Number of pages; 0 for a Word document, a workbook or a book, which have none.
Sourcepub fn analyze(&self) -> Analysis
pub fn analyze(&self) -> Analysis
Runs content extraction and layout analysis with default options.
Sourcepub fn analyze_with(&self, opts: AnalyzeOptions) -> Analysis
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).
Sourcepub fn renderer(&self) -> Option<PageRenderer<'_>>
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.
Sourcepub fn to_html(&self, analysis: &Analysis, opts: &HtmlOptions) -> HtmlOutput
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).
Sourcepub fn write_html(
&self,
analysis: &Analysis,
opts: &HtmlOptions,
out: &mut dyn Write,
file: &mut dyn FnMut(&str, &[u8]) -> Result<()>,
) -> Result<()>
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.
Sourcepub fn to_docx(&self, analysis: &Analysis, opts: &DocxOptions) -> Vec<u8> ⓘ
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.
Sourcepub fn to_docx_with_images(
&self,
analysis: &Analysis,
opts: &DocxOptions,
image_dir: &str,
) -> DocxOutput
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.
Sourcepub fn to_markdown(
&self,
analysis: &Analysis,
opts: &MarkdownOptions,
) -> MarkdownOutput
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.
Sourcepub fn to_latex(&self, analysis: &Analysis, opts: &LatexOptions) -> LatexOutput
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.
Sourcepub fn to_typst(&self, analysis: &Analysis, opts: &TypstOptions) -> TypstOutput
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.
Sourcepub fn to_epub(&self, analysis: &Analysis, opts: &EpubOptions) -> Vec<u8> ⓘ
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.
Sourcepub fn to_json(&self, analysis: &Analysis, opts: &JsonOptions) -> JsonOutput
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.
Sourcepub fn to_chunks(&self, analysis: &Analysis, opts: &ChunkOptions) -> Vec<Chunk>
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.
Sourcepub fn apply_content(
&self,
analysis: &mut Analysis,
json: &str,
) -> Result<ContentReport>
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.
Sourcepub fn apply_content_in(
&self,
analysis: &mut Analysis,
json: &str,
base: &Path,
) -> Result<ContentReport>
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
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
impl Sync for Paper
impl Unpin for Paper
impl UnsafeUnpin for Paperwhere
*const ArcInner<XRef> is !null: UnsafeUnpin,
PhantomData<ArcInner<XRef>>: UnsafeUnpin,
*const u8 is !null: UnsafeUnpin,
PhantomData<Page<'static>>: UnsafeUnpin,
&'static XRef: UnsafeUnpin,
*const ArcInner<dyn AsRef<[u8]> + Sync + Send> is !null: UnsafeUnpin,
PhantomData<ArcInner<dyn AsRef<[u8]> + Sync + Send>>: UnsafeUnpin,
*const Document is !null: UnsafeUnpin,
PhantomData<Document>: UnsafeUnpin,
PhantomData<u8>: UnsafeUnpin,
Align4<u32>: UnsafeUnpin,
PhantomData<PageInfo>: UnsafeUnpin,
PhantomData<Block>: UnsafeUnpin,
PhantomData<Picture>: UnsafeUnpin,
PhantomData<Vec<PLine>>: UnsafeUnpin,
PhantomData<Vec<RotatedRun>>: UnsafeUnpin,
PhantomData<Option<Laid>>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<U, T> ToOwnedObj<U> for Twhere
U: FromObjRef<T>,
impl<U, T> ToOwnedObj<U> for Twhere
U: FromObjRef<T>,
Source§fn to_owned_obj(&self, data: FontData<'_>) -> U
fn to_owned_obj(&self, data: FontData<'_>) -> U
T, using the provided data to resolve any offsets.