eDocEngine VCL
Create documents and reports in 18 formats
Compatibility
Delphi C++Builder

How To Convert XML To PDF Using Delphi

Using eDocEngine VCL and Pbear's THTMLViewer
By V. Subhash

If you are familiar with XML and XSLT, then you also know that transformed XML documents can be displayed inside a web browser just like a web page.

Input Documents - XML and XSL
Transformed XML In A Browser

Well, it does not stop with transformed XML. A lot of people have been asking us about exporting XML to PDF and other formats. And, here is our solution.

Our solution uses an old Delphi component called THTMLViewer. THTMLViewer is no longer under development by its original authors. The last update from them was for Delphi 2006. However, the authors had made the project open source. There is a Delphi 2009 update for the component from the good folks at www.songbeamer.com/delphi/.

Before you try our solution, you need to install the new THTMLViewer and then recompile eDocEngine's export interface component for THTMLViewer - TgtHtmlViewInterface. The test XML and XSL files used in our example code are named gnostice_nl.xsl and placed in the project folder. (You can use any XML and XSL file so long as the THTMLViewer can display it without problem.) Here are the steps for creating the Delphi application.

  1. Open the IDE and create a VCL forms application project.
  2. Drop the following components on the form.
    1. TButton
    2. TMemo
    3. THTMLViewer
    4. TgtPDFEngine
    5. TgtHtmlViewInterface
    Components for the XML-to-PDF Delphi Application
  3. Add the following click-event handler for the button.
    procedure TForm1.Button1Click(Sender: TObject);
    var
       XMLDocument1:  TXMLDocument;
       XSLPageProducer1: TXSLPageProducer;
    begin
      XMLDocument1 :=  TXMLDocument.Create(self);
      XSLPageProducer1 := TXSLPageProducer.Create(self);
    
      try
        // Transform XML and place HTML in memo
        XMLDocument1.Active := False;
        XSLPageProducer1.FileName := 'Input_Docs\gnostice_nl.xsl';
        XMLDocument1.FileName := 'Input_Docs\gnostice_nl.xml';
        XSLPageProducer1.XMLData := XMLDocument1;
        XMLDocument1.Active := True;
        Memo1.Text := XSLPageProducer1.Content;
        XMLDocument1.Active := False;
    
        // Display HTML from memo
        HTMLViewer1.LoadFromString(Memo1.Text);
    
        // Export displayed HTML to PDF
        gtHtmlViewInterface1.Engine := gtPDFEngine1;
        gtPDFEngine1.Preferences.ShowSetupDialog := false;
        gtPDFEngine1.FileName := 'Output_Docs\gnostice_nl.pdf';
        gtHtmlViewInterface1.RenderDocument(HTMLViewer1);
      except on E: Exception do
        Memo1.Text := E.Message
      end;
    
    end;
    
  4. Run the application.
  5. Click the button - to transform the XML to HTML, display the HTML in the viewer, and finally to export the displayed contents of the viewer to PDF using eDocEngine.
    XML-to-PDF Delphi Application In Action

Here is the final output.

PDF Output
Downloads

---o0O0o---

Our .NET Developer Tools
XtremeDocumentStudio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
XtremeDocumentStudio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
XtremeDocumentStudio (for Java)

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2017 Gnostice Information Technologies Private Limited. All rights reserved.