Export to PDF"

From Documentation
m (correct highlight (via JWB))
 
(11 intermediate revisions by 3 users not shown)
Line 15: Line 15:
  
 
'''Example to export as a PDF'''
 
'''Example to export as a PDF'''
<source lang="java" high="21, 32">
+
<source lang="java" highlight="21, 32">
 
package org.zkoss.zss.essential;
 
package org.zkoss.zss.essential;
  
Line 62: Line 62:
  
 
= Export Server Setup =
 
= Export Server Setup =
  Since 3.6.1
+
  Since 3.7.0
 +
Since 3.7.0, the default chart engine has become ZK Charts. With this engine, exporting spreadsheets that include charts to PDF needs extra export server chart data rendering at the server side. The easiest way is to adopt Highcharts official solution which is based on PhantomJS, a headless Webkit browser.
  
Since 3.6.1, the default chart engine has became ZK Charts. With this engine, exporting to PDF needs extra export server to generate image from concrete chart data at server side. The easiest way is adopting Highcharts official solution which is based on PhantomJS , a headless Webkit browser.
+
# download and install PhantomJS at http://phantomjs.org/download.html.
 
+
# download Highcharts project at http://www.highcharts.com/download.
# download and install PhantomJS at http://phantomjs.org/download.html
 
# download Highcharts project.
 
 
## in this project, go to exporting-server -> phantomjs
 
## in this project, go to exporting-server -> phantomjs
# start export server "phantomjs highcharts-convert.js -host 127.0.0.1 -port 3003"
+
# start export server by commanding "phantomjs highcharts-convert.js -host 127.0.0.1 -port 3003"
 +
## you should move the PhantomJS executable file into this folder
 +
### On Windows, the full name is phantomjs.exe
 +
### On Mac or Linux, the full name is phantomjs
 
## you can customize host and port respectively by parameter -host and -port
 
## you can customize host and port respectively by parameter -host and -port
 
# add properties into zk.xml
 
# add properties into zk.xml
 
## there are three library properties to be used
 
## there are three library properties to be used
  
This is the location to export server. When it doesn't find this definition, JFreechart will be used to generate chart graph. (Necessary)
+
'''Library properties about export server'''
 +
 
 +
This is the location to export server. When it doesn't find this definition, JFreechart will be used to generate chart graphs. (Required)
 
<source lang="xml" >
 
<source lang="xml" >
 
<library-property>
 
<library-property>
Line 82: Line 86:
 
</source>
 
</source>
  
Scale is a zoom factor that affect pixel density than original. 2 means double resolution than original. (Optional)
+
Scale is a zoom factor that affects pixel density relative to the original, for example, 2 means double the resolution of the original. (Optional)
 
<source lang="xml" >
 
<source lang="xml" >
 
<library-property>
 
<library-property>
Line 98: Line 102:
 
</source>
 
</source>
  
After restarting server, you will see the ZK Charts based chart when exporting PDF.
+
After restarting server, ZK Charts will be shown when exporting PDF.
  
 
= Load Excel Printing Setup =
 
= Load Excel Printing Setup =
Line 107: Line 111:
 
[[File:ExcelPageSetup.png | center]]
 
[[File:ExcelPageSetup.png | center]]
  
For example you can add header and footer, and it would look like:
+
For example, you can add a header and footer, and it would look like:
  
 
[[File:pdfExporting-HeaderFooter.png | center]]
 
[[File:pdfExporting-HeaderFooter.png | center]]
Line 115: Line 119:
 
[[File: pdfExporting-FitOnePage.png | center]]
 
[[File: pdfExporting-FitOnePage.png | center]]
  
== Supported Page Setup ==
+
= Supported Page Setup =
 
The page setup properties of Excel which are supported by Spreadsheet are listed below:
 
The page setup properties of Excel which are supported by Spreadsheet are listed below:
  
Line 144: Line 148:
 
* Different odd and even pages
 
* Different odd and even pages
 
* Different first page
 
* Different first page
 +
Custom header and footer is supported since 3.8.1
  
 
'''Sheet'''
 
'''Sheet'''

Latest revision as of 12:52, 19 January 2022




PDF Exporter

Available in ZK Spreadsheet EE only


In addition to the Excel format, you can also export a book model as a PDF file with Exporter

Example to export as a PDF

package org.zkoss.zss.essential;

import java.io.*;

import org.zkoss.util.media.AMedia;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.*;
import org.zkoss.zss.api.*;
import org.zkoss.zss.api.model.Book;
import org.zkoss.zss.ui.Spreadsheet;
import org.zkoss.zul.*;

public class ExportPdfComposer extends SelectorComposer<Component> {

	@Wire
	private Spreadsheet ss;
	@Wire("combobox")
	private Combobox combobox;

	Exporter exporter = Exporters.getExporter("pdf");
	
	
	@Listen("onClick = #exportPdf")
	public void doExport() throws IOException{
		Book book = ss.getBook();
		
		File file = File.createTempFile(Long.toString(System.currentTimeMillis()),"temp");
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			exporter.export(book, file);
		}finally{
			if(fos!=null){
				fos.close();
			}
		}
		Filedownload.save(new AMedia(book.getBookName()+".pdf", "pdf", "application/pdf", file, true));
	}

	//omit some code for brevity
}

Line 21: Get an Exporter instance for PDF format.

Export Server Setup

Since 3.7.0

Since 3.7.0, the default chart engine has become ZK Charts. With this engine, exporting spreadsheets that include charts to PDF needs extra export server chart data rendering at the server side. The easiest way is to adopt Highcharts official solution which is based on PhantomJS, a headless Webkit browser.

  1. download and install PhantomJS at http://phantomjs.org/download.html.
  2. download Highcharts project at http://www.highcharts.com/download.
    1. in this project, go to exporting-server -> phantomjs
  3. start export server by commanding "phantomjs highcharts-convert.js -host 127.0.0.1 -port 3003"
    1. you should move the PhantomJS executable file into this folder
      1. On Windows, the full name is phantomjs.exe
      2. On Mac or Linux, the full name is phantomjs
    2. you can customize host and port respectively by parameter -host and -port
  4. add properties into zk.xml
    1. there are three library properties to be used

Library properties about export server

This is the location to export server. When it doesn't find this definition, JFreechart will be used to generate chart graphs. (Required)

<library-property>
	<name>org.zkoss.zss.chart.render.server.url</name>
	<value>http://127.0.0.1:3003</value>
</library-property>

Scale is a zoom factor that affects pixel density relative to the original, for example, 2 means double the resolution of the original. (Optional)

<library-property>
	<name>org.zkoss.zss.chart.render.server.scale</name>
	<value>1</value>
</library-property>

Default timeout is 10000 milliseconds (10 seconds) for waiting response from export server. (Optional)

<library-property>
	<name>org.zkoss.zss.chart.render.server.timeout</name>
	<value>10000</value>
</library-property>

After restarting server, ZK Charts will be shown when exporting PDF.

Load Excel Printing Setup

Since 3.6.0

Spreadsheet exports its book model to a PDF file according to the page print setup you specify in Excel.

ExcelPageSetup.png

For example, you can add a header and footer, and it would look like:

PdfExporting-HeaderFooter.png


You can also scale a sheet to fit into one page with row and column heading:

PdfExporting-FitOnePage.png

Supported Page Setup

The page setup properties of Excel which are supported by Spreadsheet are listed below:

Page

  • Orientation
    • Portrait, Landscape
  • Scaling
    • Adjust to % normal size
    • Fit to pages wide by tall
  • Page size
  • First page number

Margins

  • Top
  • Header
  • Right
  • Footer
  • Bottom
  • Left

Center on Page

  • Horizontally
  • Vertically

Header/Footer

  • Custom Header...
  • Custom Footer...
  • Different odd and even pages
  • Different first page
Custom header and footer is supported since 3.8.1

Sheet

  • Print area
  • Print titles
    • Rows to repeat at top
    • Columns to repeat at left
  • Print
    • Gridlines
    • Row and column headings
  • Page order
    • Down, then over
    • Over, then down


All source code listed in this book is at Github.


Last Update : 2022/01/19

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.