0

Best way to show pdf popup window

asked 2009-08-13 09:26:15 +0800

guleri gravatar image guleri
75 2

Hi
I'm wondering what is the best way to present an pdf file on screen i generate in my application.

My app have a lot of state in the "page" and i need a button to show av pdf file in a new window based on the information in my application (in memory). (By the way, my zul file is mainly an empty shell with java classes doing the work and UI, if that makes it any harder/easier)
I thougth of sending it to a new window and url building the pdf, but how do i elegantly pull the data needed?.

I also came over Filedownload.save() wich i might use?

Thanks..!
-Erik

delete flag offensive retag edit

5 Replies

Sort by ยป oldest newest

answered 2009-08-13 11:34:44 +0800

guleri gravatar image guleri
75 2

Hmm, i tried the Filedownload.save() with work if i select "open", except it is absolutly useless in IE. Asking if you really want to download the file, and reloads the current page... removing mye state and i need to retry it to manage to get anything down.. :-\
I was thinking maybe posting an xml string to another page converting it to a pdf. Not sure how to do this though. How can i send a http post to a new window?

link publish delete flag offensive edit

answered 2009-08-13 11:37:38 +0800

iantsai gravatar image iantsai
2755 1

What about using iframe?

you can call an iframe to link a resource(PDF, PNG, etc...) by URL.

link publish delete flag offensive edit

answered 2009-08-14 14:23:13 +0800

guleri gravatar image guleri
75 2

This is not really the problem. My content is dynamic from my zk-application and not a file on the server to download.
The pdf will be a new request from the client to a servlet or something i guess, but I would like a good way to access my data from the running zk app in memory, or "push"/control the whole process from zk.
If i can't find a good solution i think i might end up passing an xml in session to be cleaned up after client have downloaded the pdf i generate. just feel like it is a litt clomsy.

link publish delete flag offensive edit

answered 2009-08-14 17:42:06 +0800

caclark gravatar image caclark
1753 2 5
http://clarktrips.intltwi...

Why can't you do as iantsai suggests and invoke your "servlet or something" that generates the PDF? Have it set the content type via the response object (assuming you're using Java - other server side technologies have something similar) and then write the PDF to the output stream. Are you using something that gives you a byte array of the PDF in memory? If so, just cram it on the output stream, flush it, and close. The browser will fire up the configured PDF handler and you're done.

Make you a simple ZUL file like this:

<?xml version="1.0" encoding="windows-1251"?>

<zk xmlns="http://www.zkoss.org/2005/zul"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd">

    <window id="pdfDisplayWindow" width="640px" height="480px" border="normal" 
            visible="true" left="130px" top="55px" closable="true" sizable="true" maximizable="true"
            xmlns:a="http://www.zkoss.org/2005/zk/annotation" >
        <caption label="My PDF Viewer"/>

        <borderlayout height="100%">
            <center>
                <iframe style="width:99%; height:400px;border:3px inset;" src="/myServerCodeToGenerateAPdf" />
            </center>
            <south height="30px">
                <hbox>
                    <button id="closeButton" label="Close" forward="onClick=pdfDisplayWindow.onClose(event)"/>
                </hbox>
            </south>
        </borderlayout>
    </window>
</zk>

link publish delete flag offensive edit

answered 2009-08-14 18:54:31 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

Don't exactly the problem you discribed but here is my ReportWindow that i'm using to
show JasperReports.

JReportWindow.java

package org.myfirm.webui.reports.util;

import java.io.Serializable;
import java.util.Map;

import net.sf.jasperreports.engine.JRDataSource;

import org.apache.log4j.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zkex.zul.Jasperreport;
import org.zkoss.zul.Window;

public class JRreportWindow extends Window implements Serializable {

	private static final long serialVersionUID = -5587316458377274805L;
	private transient static final Logger logger = Logger.getLogger(JRreportWindow.class);

	private transient Jasperreport report;

	/* The parent that calls the report */
	private transient Component parent;

	/* if true, shows the ReportWindow in ModalMode */
	private transient boolean modal;

	/* report params like subreports, title, author etc. */
	private transient Map reportParams;

	/* reportname with whole path must ends with .jasper (compiled) */
	private transient String reportPathName;

	/* JasperReports Datasource */
	private transient JRDataSource ds;

	/* 'pdf', 'xml', .... */
	private transient String type;

	/**
	 * Constructor.<br>
	 * <br>
	 * Creates a report window container.<br>
	 * 
	 * @param parent
	 * @param modal
	 * @param reportParams
	 * @param reportPathName
	 * @param ds
	 * @param type
	 */
	public JRreportWindow(Component parent, boolean modal, Map reportParams, String reportPathName, JRDataSource ds, String type) {
		super();
		this.parent = parent;
		this.modal = modal;
		this.reportParams = reportParams;
		this.reportPathName = reportPathName;
		this.ds = ds;
		this.type = type;

		createReport();
	}

	private void createReport() {

		if ((Boolean) modal == null) {
			modal = true;
		}

		if (reportPathName.isEmpty()) {
			// throw new FileNotFoundException(reportPathName, "kjhkjhkj");
		}

		if (ds == null) {
			// throw new FileNotFoundException(reportPathName, "kjhkjhkj");
		}

		if (type.isEmpty()) {
			type = "pdf";
		}

		report = new Jasperreport();
		report.setSrc(reportPathName);
		report.setParameters(reportParams);
		report.setDatasource(ds);
		report.setType(type);

		// Window win = new Window();
		this.setParent(parent);

		System.out.println("wwwwwwwwwwwwwwwwwwwwwwwwwwwww " + parent.getRoot().toString());

		this.setTitle("JasperReports Sample Report for ZKoss");
		this.setVisible(true);
		this.setMaximizable(true);
		this.setMinimizable(true);
		this.setSizable(true);
		this.setClosable(true);
		this.setHeight("100%");
		this.setWidth("80%");

		this.addEventListener("onClose", new EventListener() {
			@Override
			public void onEvent(Event event) throws Exception {
				closeReportWindow();
			}
		});

		this.appendChild(report);

		if (modal == true) {
			try {
				this.doModal();
			} catch (SuspendNotAllowedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	/**
	 * We must clear something to prevent errors or problems <br>
	 * by opening the report several times. <br>
	 */
	private void closeReportWindow() {
		if (logger.isDebugEnabled()) {
			logger.debug("detach Report and close ReportWindow");
		}
		report.removeEventListener("onClose", new EventListener() {
			@Override
			public void onEvent(Event event) throws Exception {
				closeReportWindow();
			}
		});

		report.detach();
		this.onClose();

	}
}

regards
Stephan

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2009-08-13 09:26:15 +0800

Seen: 2,466 times

Last updated: Aug 14 '09

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More