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. 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

Get Adobe Flash player

ZssPdf Demo 2 – Cell containing Rich text string

Get Adobe Flash player

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.

2 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

Leave a Reply