ZK Spreadsheet delivers rich functionality of Excel® within browsers thereby allowing developers to create collaborative and dynamic enterprise applications. Sometimes users of these enterprise applications need to export these collaborative spreadsheet contents for offline reading, reporting & filing or sending as email attachments. With the release of ZK Spreadsheet 2.0 beta3 we have added a new feature, ZssPdf, which fulfills this particular need. If you do not like the ZssPdf, check out soda PDF as the software is designed to meet your business needs. ZssPdf enables enterprise developers to add export to a pdf ability to their enterpsie application built using ZK Spreadsheet.

Below are two simple demonstrations of spreadsheet export to PDF format. Although in the demos I am using MS Excel spreadsheet as an input the feature works seamlessly with the ZK Spreadsheet component.

ZssPdf Demo 1 – Cell with various styles

ZssPdf Demo 2 – Cell containing Rich text string

As you can see ZssPdf supports exporting highly stylized spreasheet contents including merged cells, rich text cell contents, cell background colors, various border styles & border colors and embedded images. In addtion to content styles ZssPdf also supports various print related functions such as printing of grid lines, row/column headings, different paper sizes, orientation (Landscape/Portrait), custom margins, header & footer printing and so on.

So how do I use it?
Lets take a look at the demo source code

    public void onUpload$xlsUploadBtn(UploadEvent event)
            throws InvalidFormatException, IOException, InterruptedException {

        File tempDir = createTempDir();

        OutputStream os = null;
        // get uploaded xls file
        org.zkoss.util.media.Media media = event.getMedia();
        if (media.isBinary()) {
            String outputFilePath = tempDir.getAbsolutePath()
                    + System.getProperty("file.separator")
                    + System.currentTimeMillis() + ".pdf";

            Importer importer = new ExcelImporter();
            Book wb = importer.imports(media.getStreamData(), media.getName());

            Exporter c = new PdfExporter();
            ((PdfExporter) c).enableHeadings(printHeadings.isChecked());

            os = new java.io.FileOutputStream(outputFilePath);
            c.export(wb, os);


            final InputStream mediais = new FileInputStream(new File(
            final AMedia amedia = new AMedia("generatedReport.pdf", "pdf",
                    "application/pdf", mediais);

        } else {
            Messagebox.show("Not a binary file: " + media, "Error",
                    Messagebox.OK, Messagebox.ERROR);

        // remove temp dir containing pdf output

Well as it turns out it is very easy to use this feature. In 2.0 beta3 release of ZK Spreadsheet you will see a generic interface org.zkoss.zss.model.Exporter which has a single method org.zkoss.zss.model.Exporter#export(Book , OutputStream). This method needs ZK Spreadsheet model org.zkoss.zss.model.Book representing particular spreadsheet and java.io.OutputStream to which the exported PDF format contents are to be written. Also there is org.zkoss.zss.model.impl.PdfExporter an implementation of this interface that implements export to PDF format function.

In the above source code I get spreadsheet model for uploaded spreadsheet file using org.zkoss.zss.model.Importer#imports(InputStream,String) method and export its contents by instantiating PdfExporter & invoking its org.zkoss.zss.model.Exporter#export(Book , OutputStream) implementation. Note that I passed an OutputStream to a temp PDF file to which the exported contents are written. Once exported, contents of this temporary PDF file are rendered back to the client and finally temporary PDF file is removed from server.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

6 Responses to “Export ZK Spreadsheet to PDF”

  1. kareppa says:

    sir i want to retive tha data from database and display in pdf format

  2. kareppa says:

    plz its urgent

  3. daquan198163 says:

    Can not find PdfExporter in the latest version of zk-spreadsheet,where is it?

  4. jeanyen says:

    PDF export is a ZK Spreadsheet EE feature. If you use maven, don’t forget to include the zsspdf dependency. Sample pom here

  5. daquan198163 says:

    PdfExporter.export requires a SBook as first parameter, but importer.imports returns a Book instance.

  6. jeanyen says:

    This article is created in year 2010 for ZK Spreadsheet 2 and may not be applicable now. Did you reference the latest document here? https://www.zkoss.org/wiki/ZK_Spreadsheet_Essentials/Working_with_Spreadsheet/Handling_Data_Model/Export_to_PDF

Leave a Reply