PdfElixide.Office (pdf_elixide v0.23.0)

Copy Markdown View Source

Converts Word, PowerPoint and Excel files to PDF.

to_pdf/1 takes the bytes of a .docx, .pptx or .xlsx file and returns the bytes of a new PDF. Open the result to read it, or write it out as it is:

{:ok, pdf} = PdfElixide.Office.to_pdf(File.read!("report.docx"))
File.write!("report.pdf", pdf)

The format is read from the file's own package, not from a name or an argument, so macro-enabled and template variants (.docm, .dotx, .pptm, .ppsx, .xlsm, …) are accepted too. The older binary formats (.doc, .ppt, .xls) and password-protected files are not.

To convert the other way, see PdfElixide.Document.to_docx/2, PdfElixide.Document.to_pptx/2 and PdfElixide.Document.to_xlsx/2. The Office conversion guide covers both directions, including the fallback font files an import looks for at fixed paths.

Summary

Functions

Converts a DOCX, PPTX or XLSX file to a new PDF and returns the PDF's bytes.

Converts a DOCX, PPTX or XLSX file to a new PDF, raising an error if it fails.

Functions

to_pdf(bytes)

@spec to_pdf(binary()) :: {:ok, binary()} | {:error, PdfElixide.Error.t()}

Converts a DOCX, PPTX or XLSX file to a new PDF and returns the PDF's bytes.

Bytes that cannot be read as one of those packages — including a PDF, a zip file holding something else, a truncated or corrupted zip, or a password-protected Office file — return {:error, %PdfElixide.Error{reason: :unsupported}}. A package that is recognised but cannot be converted returns {:error, %PdfElixide.Error{}} with reason :other.

A DOCX exported with mode: :layout, or with the default :auto on a short document, does not keep its pages when converted back; see Round trips.

Memory follows the size of the package once unzipped, not the size of the upload; see Memory and concurrency before converting untrusted files.

to_pdf!(bytes)

@spec to_pdf!(binary()) :: binary()

Converts a DOCX, PPTX or XLSX file to a new PDF, raising an error if it fails.