Conversion
Convert PDFs to Markdown, Word, Excel, PowerPoint and HTML, and create PDFs from Office files, HTML, e-books and images with NexPDF for Flutter.
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.
Markdown
toMarkdown() keeps headings, lists and tables:
final markdown = doc.toMarkdown();Images to PDF
sdk.open opens JPEG, PNG and TIFF pictures as a PDF with one page per picture (multi-page TIFF
too):
final pdf = sdk.open(await File(photoPath).readAsBytes());
final scans = pdf.save();PDF to Office and HTML
The conversion functions have no Dart method. Call them
through dart:ffi on doc.handle:
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'nexpdf_ffi.dart';
typedef _DocBytes = NexPdfBytes Function(Pointer<Void>);
typedef _DocString = Pointer<Utf8> Function(Pointer<Void>);
final toDocx = nexpdfLib.lookupFunction<_DocBytes, _DocBytes>('nexpdf_to_docx'); // NexPdfBytes nexpdf_to_docx(const NexPdfDocument *doc);
final toXlsx = nexpdfLib.lookupFunction<_DocBytes, _DocBytes>('nexpdf_to_xlsx');
final toHtml = nexpdfLib.lookupFunction<_DocString, _DocString>('nexpdf_to_html'); // char *nexpdf_to_html(const NexPdfDocument *doc);
final docx = nexpdfCall(() => takeBytes(toDocx(doc.handle)));
final xlsx = nexpdfCall(() => takeBytes(toXlsx(doc.handle))); // one sheet per page, tables rebuilt
final html = nexpdfCall(() => takeString(toHtml(doc.handle)));The other converters have the same signatures:
| C function | Output |
|---|---|
nexpdf_to_docx, nexpdf_to_xlsx, nexpdf_to_pptx | Word, Excel (one sheet per page), PowerPoint (one slide per page), as NexPdfBytes |
nexpdf_to_html, nexpdf_to_json, nexpdf_to_xml, nexpdf_to_csv, nexpdf_to_rtf | HTML, JSON, XML, the tables as CSV, RTF, as char * |
nexpdf_to_reflow_html(doc, options_json) | Responsive HTML for small screens |
Office, HTML and e-books to PDF
nexpdf_open_office(data, len), nexpdf_open_ebook(data, len) (EPUB, FictionBook, XPS/OpenXPS,
CBZ) and nexpdf_from_html(html) return a new native document. The Dart NexPdfDocument cannot
wrap it, so save it with nexpdf_save_bytes, free it with nexpdf_free, and open the bytes with
sdk.open:
// NexPdfDocument *nexpdf_open_office(const uint8_t *data, size_t len);
final openOffice = nexpdfLib.lookupFunction<Pointer<Void> Function(Pointer<Uint8>, Size),
Pointer<Void> Function(Pointer<Uint8>, int)>('nexpdf_open_office');
final saveBytes = nexpdfLib.lookupFunction<_DocBytes, _DocBytes>('nexpdf_save_bytes');
final free = nexpdfLib.lookupFunction<Void Function(Pointer<Void>), void Function(Pointer<Void>)>('nexpdf_free');
final pdfBytes = using((a) {
final raw = nexpdfCall(() => openOffice(toNative(docxBytes, a), docxBytes.length));
try {
return takeBytes(saveBytes(raw));
} finally {
free(raw);
}
});
final proposal = sdk.open(pdfBytes);The native reader also opens Word, Excel and PowerPoint files directly, and exports to Word, Excel and PowerPoint from its More menu.
Word options
nexpdf_open_docx_with_options_json converts a Word file with options. It has no typed Dart
method; through sdk.invoke it returns an invoke handle, which you save and close before opening
the bytes with sdk.open:
import 'dart:convert';
final h = sdk.invoke('open_docx_with_options_json',
[docxBytes, jsonEncode({'show_comments': true, 'show_revisions': true})]) as int;
final pdfBytes = base64Decode(sdk.invoke('save_bytes', [h]) as String);
sdk.invoke('close', [h]);
final reviewed = sdk.open(pdfBytes);| Option | Default | Meaning |
|---|---|---|
show_comments | false | Draw the comments in balloons in a markup area at the right of each page (the page content is scaled down, as Word prints with markup) |
show_revisions | false | Show 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 replies
and the resolved state kept. Without show_revisions the document is converted as it reads after
accepting every change.
Word conversion also lays out section columns (equal or unequal, with separators, column breaks and continuous section breaks), conditional table styles (first and last rows and columns, banding), tight and through wrapping along the wrap polygon, floating objects in table cells and text boxes, and tables in text boxes.
Excel formulas and a fixed clock
Formulas without a cached value are calculated; cached values still win. The calculation follows
references across sheets, defined names, shared and array formulas and dynamic arrays that spill
(#SPILL! when blocked), with Excel's error values and about 280 functions: maths, statistics
(including the *IFS family), lookup (XLOOKUP, INDEX, OFFSET, INDIRECT), text (including
TEXT format codes), dates and times, logic, finance, information, FILTER, SORT, UNIQUE,
SEQUENCE, and LET and LAMBDA (named and recursive) with MAP, REDUCE, SCAN, MAKEARRAY,
BYROW, BYCOL and ISOMITTED. Circular references show 0 as Excel does, or iterate when the
workbook turns on iterative calculation. Conditional formatting draws every built-in and x14 icon
set and x14 data bars (negative colours, axis, direction, gradient, border).
NOW() and TODAY() use the current time. For output that does not change from run to run, fix
the clock in Unix seconds (no time zone is applied):
final h = sdk.invoke('open_xlsx_with_clock', [xlsxBytes, 1796000000]) as int; // then save_bytes and closeRight-to-left and complex scripts
Word, Excel, PowerPoint and HTML conversion shapes text with OpenType (Arabic joining, Indic
reordering, marks, ligatures) and orders it with the Unicode bidi algorithm: Word w:bidi and
w:rtl, Excel right-to-left sheets and readingOrder, PowerPoint right-to-left paragraphs and HTML
dir and direction.
Page images
To export pages as PNG, see Rendering.