Skip to Content
NexPDF

Conversion

Convert PDFs to Word, Excel, PowerPoint, HTML, JSON, XML, CSV and RTF, and create PDFs from Office files, HTML, e-books and images with NexPDF for Rust.

English

License module

Conversion to and from Office, HTML, e-book and data formats needs the conversion module (Startup tier and above). Images to PDF and Markdown export need no module.

PDF to Office

use nexpdf_sdk::Document;

fn main() -> nexpdf_sdk::Result<()> {
    let doc = Document::open("report.pdf")?;
    std::fs::write("report.docx", doc.to_docx()?)?;
    std::fs::write("report.xlsx", doc.to_xlsx()?)?;   // one sheet per page, tables rebuilt
    std::fs::write("report.pptx", doc.to_pptx()?)?;   // one slide per page
    Ok(())
}

PDF to text formats

MethodOutput
to_html()HTML
to_reflow_html(options_json)Responsive HTML for small screens
to_json()The document content as JSON (to_json_value() with the serde feature)
to_xml()The document content as XML
to_csv()The tables as CSV
to_rtf()RTF
extract_markdown()Markdown (no module needed)

Each returns a String.

let html = doc.to_reflow_html(r#"{"images":true,"title":"Report"}"#)?;

Office, HTML and e-books to PDF

let pdf = Document::open_office(&std::fs::read("proposal.docx")?)?;   // Word, Excel or PowerPoint, format detected
pdf.save("proposal.pdf")?;

let invoice = Document::from_html("<h1>Invoice 1042</h1><p>Total: 120 EUR</p>")?;
invoice.save("invoice.pdf")?;

let book = Document::open_ebook(&std::fs::read("book.epub")?)?;      // EPUB, FictionBook, XPS/OpenXPS, CBZ

Document::open and Document::from_bytes also convert Word, Excel and PowerPoint files, e-books and comics directly; nexpdf_sdk::zip_kind tells which converter a buffer goes to.

Word: comments and tracked changes

Document::open_docx_with_options_json(data, options_json) converts a .docx with options (None or "" = defaults):

let pdf = Document::open_docx_with_options_json(
    &std::fs::read("review.docx")?,
    r#"{"show_comments":true,"show_revisions":true}"#,
)?;
pdf.save("review-markup.pdf")?;
OptionDefaultMeaning
show_commentsfalseDraws comments in balloons in a markup area at the right of each page, with the page content scaled down, as Word prints with markup
show_revisionsfalseShows tracked changes as markup (coloured underlined insertions, struck deletions, change bars) instead of the final document

Comments are always exported as PDF text annotations anchored at the commented text, with their replies and resolved state, whatever show_comments says.

Word conversion also lays out section columns (equal and unequal widths, separators, column breaks and continuous section breaks), conditional table styles (tblStylePr with tblLook and banding), tight and through wrapping along wrap polygons, floats in table cells and text boxes, and tables in text boxes.

Excel: formulas

Formulas without a cached value are calculated; cached values still win. The calculation covers references across sheets, defined names, shared and array formulas, dynamic arrays that spill (#SPILL!), Excel error values and about 280 functions: maths, statistics (with the *IFS functions), lookup (XLOOKUP, INDEX, OFFSET, INDIRECT), text (with TEXT format codes), dates and times, logic, finance, information, and FILTER, SORT, UNIQUE and SEQUENCE. LET and LAMBDA (named and recursive), MAP, REDUCE, SCAN, MAKEARRAY, BYROW, BYCOL and ISOMITTED work too. Circular references show 0, as Excel does, or iterate per the workbook's iterative-calculation settings (calcPr iterate, count and delta). Conditional formatting draws every built-in and x14 icon set and x14 data bars (negative colours, axis, direction, gradient, border).

For reproducible output, Document::open_xlsx_with_clock(data, clock_unix_seconds) makes NOW() and TODAY() return a fixed time (seconds from 1970-01-01 00:00, no time zone applied):

let pdf = Document::open_xlsx_with_clock(&std::fs::read("budget.xlsx")?, 1_795_000_000.0)?;

Right-to-left and complex scripts

Word, Excel, PowerPoint and HTML conversion shape text with OpenType (Arabic joining, Indic reordering, marks, ligatures) and order it with the full Unicode bidi algorithm: w:bidi and w:rtl in Word, right-to-left sheets and readingOrder in Excel, rtl paragraphs in PowerPoint, and dir and direction in HTML.

Images to PDF

let mut pdf = Document::open_image(&std::fs::read("scan.tiff")?)?;   // one page per picture (multi-page TIFF too)

let count = pdf.page_count()? as usize;
pdf.insert_image_pages(count, &std::fs::read("photo.jpg")?)?;        // append more pictures
pdf.save("scans.pdf")?;

Page images

To export pages as PNG, see Rendering.

On this page