🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
PDF
This page was created by Hans.karlsen on 2018-05-14. Last edited by Wikiadmin on 2026-07-29.

You can deliver MDriven-generated OpenDocument reports as PDF files by choosing a conversion approach that matches where the conversion runs: a local WPF client, Turnkey, or MDrivenServer.

Start with the report output

MDriven reports are generated from OpenDocument templates as:

  • .odt for text documents.
  • .ods for spreadsheets.

Create the report template and generate the OpenDocument output as described in Training:Microsoft office and OpenDocument as a Report generator. PDF is a conversion step performed after the report has been generated.

For example, an invoice template can generate Invoice-10042.odt. You can then convert that file to Invoice-10042.pdf before offering it for download.

Choose a conversion approach

Where the conversion runs Recommended approach Why
WPF client with Microsoft Word installed locally Microsoft Word automation Word is available on the local machine. This approach is appropriate when the client can access the Word installation.
Turnkey or MDrivenServer running under IIS A server-compatible .NET PDF component, or LibreOffice through Documentation:SysDocBatch Do not automate Microsoft Word inside IIS. Word components do not work well when called from IIS.
Turnkey or MDrivenServer where LibreOffice is installed LibreOffice with Documentation:SysDocBatch SysDocBatch can collect generated documents and convert supported OpenDocument files before download.

Convert documents on the server

For server-side conversion, use one of the following supported patterns rather than Word automation:

  • Use a commercial .NET component as shown in Documentation:Serverside PDF. The example there uses Aspose.Words and stores the source document and resulting PDF in Blob values.
  • Install and configure LibreOffice, then use Documentation:SysDocBatch to convert documents in a batch. LibreOffice can convert .odt to PDF and can also convert OpenDocument files to Microsoft Office formats as documented on that page.

Use SysDocBatch for generated report collections

SysDocBatch is a mergeable model pattern for collecting documents, converting them, zipping them, and controlling their download. Keep the SysDocBatch and SysDoc classes transient.

Use it when an action creates one or more reports and you want the user to choose a later conversion or download step.

  1. Merge the SysDocBatch pattern into your model from TK Live View.
  2. Install LibreOffice on the server if you will use LibreOffice conversion.
  3. Set SysMDrivenMiscSettingsSingleton.oclSingleton.LibreOfficeInstallPathandExe to the full path of soffice.exe.
  4. Generate your .odt report from a Template ViewModel.
  5. Add the generated SysDoc objects to one SysDocBatch.
  6. Open SysDocBatchView and use its PDF conversion option.
  7. Download the converted document, or zip the batch when you need one download containing several documents.

For example, a monthly billing action can generate one .odt report per customer. Instead of starting a download for each report, add the reports to one batch, convert the documents to PDF, and provide a single ZIP download.

Configure LibreOffice deliberately

LibreOffice is a separate product with its own license and must be installed on the server that runs Turnkey. After installation, configure the full executable path, for example C:\Program Files\LibreOffice\program\soffice.exe, in SysMDrivenMiscSettingsSingleton.LibreOfficeInstallPathAndExe.

On a locked-down server, disable LibreOffice online updates. Also disable Java support to reduce per-document load time:

  1. In LibreOffice, open Options with Alt+F12.
  2. Select LibreOffice, then Advanced.
  3. Clear Use Java runtime environment.
  4. Select OK.

Follow Use LibreOffice for PDF Conversion for the full setup and conversion details.

Automate Microsoft Word from a local client

Use Word automation only where Word is installed locally and can be accessed by the client, such as a WPF client. Do not use this pattern for Turnkey or MDrivenServer code running inside IIS.

The following C# example opens each existing .odt file, saves it as PDF when pdf is true, closes the document, deletes the original file, and updates file.FileName to the PDF path.

Microsoft.Office.Interop.Word.Application word =
    new Microsoft.Office.Interop.Word.Application();

foreach (var file in files)
{
    if (System.IO.Path.GetExtension(file.FileName.ToLower()) == ".odt")
    {
        // Avoid processing the same or a missing file.
        if (File.Exists(file.FileName))
        {
            _Document doc = word.Documents.Open(file.FileName);
            doc.Activate();

            string newfile = file.FileName;
            if (pdf)
            {
                newfile = System.IO.Path.ChangeExtension(file.FileName, ".pdf");
                doc.SaveAs2(newfile, WdSaveFormat.wdFormatPDF);
            }

            object oMissing = System.Reflection.Missing.Value;
            doc.Close(ref oMissing, ref oMissing, ref oMissing);

            File.Delete(file.FileName);
            file.FileName = newfile;
        }
    }
}

Microsoft.Office.Interop.Word._Application app = word.Application;
app.Quit();

Word automation prerequisites and consequences

Before using this code, ensure that:

  • Microsoft Word is installed on the computer that runs the code.
  • The process has access to the document paths.
  • The input file is an .odt file.
  • You want to delete the original .odt after conversion. The example calls File.Delete(file.FileName).

If you need to retain the source report, remove or change the deletion step. If you need server-side conversion, use Documentation:Serverside PDF or LibreOffice conversion instead.

Store and download generated PDFs

A converted PDF can be stored in a Blob attribute. When you show a PDF Blob in a ViewModel, use Documentation:Column.BlobDownloadLink rather than placing heavy Blob data directly in a grid. This lets the user download a selected document without loading every document Blob for every grid row.

For example, show the document name in one column and a Blob download link in another. The user retrieves the PDF they select, rather than causing a list of 100 document rows to load 100 PDF files.

PDF and XPS are different outputs

PDF conversion of generated reports is separate from model documentation publishing. Documentation:Documtr and XPS describes how Documtr compiles model documentation into a FlowDocument that can be saved as XPS.

See also