PDA

View Full Version : Gwt+pdf



info++
22 Dec 2011, 7:59 AM
how print data in a pdf file with gwt

gishmo
23 Dec 2011, 4:36 AM
With pure GWT it won't work.

you have two opporunities:
Send your request to your server, generate the pdf and send it back to the client
Use something like GWT4Air

info++
26 Dec 2011, 11:24 PM
Can u please give me an example with gwt4air but with the calling service solution didn't work

DavidHoffer
27 Dec 2011, 8:25 AM
I've did this with the following client code (server generated the pdf):

String url = URL.encode(GWT.getModuleBaseURL() + "pdf?id=" + result + "&presentationtype=pdf");
Frame frame = new Frame(url);
frame.setWidth("95%");
frame.setHeight(height);
setResult(frame);

Where setResult sets the frame in the current page. Users can print/save from the displayed pdf.

info++
27 Dec 2011, 8:31 AM
it dosent work for me and i dont know why maybe a reboot will fixe it :D:D:D but i create a pdf file with itext it s easy and free

DavidHoffer
27 Dec 2011, 8:55 AM
You will need the backend servlet that server the requested pdf to the client. E.g.

<servlet>
<servlet-name>pdf</servlet-name>
<servlet-class>com.qsd.server.servlet.PdfServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>pdf</servlet-name>
<url-pattern>/com.qsd.Calc/pdf</url-pattern>
</servlet-mapping>

public class PdfServlet extends HttpServlet {

private static final long serialVersionUID = 1936378722615937262L;
private static final IFileUtility fileUtility = new FileUtility();
private final Logger log;
public static final String PDF_FOLDER = System.getProperty("java.io.tmpdir") + "/" + "0CF6E3F7-DD81-634E-D119-8CD6564E1013";

public PdfServlet(Logger log) {
this.log = log;
}

public PdfServlet() {
this(Logger.getLogger(PdfServlet.class));
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {

final String presentationtype = request.getParameter("presentationtype");
final String guid = request.getParameter("id");

InputStream is = null;
ServletOutputStream outputStream = null;
try {
if ("pdf".equals(presentationtype)) {
response.setContentType("application/pdf");
final String filename = String.format("%s/%s.pdf", PDF_FOLDER, guid);
is = fileUtility.getInputStream(filename);
byte[] buffer = new byte[4096];
int len;
outputStream = response.getOutputStream();
while ((len = is.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
outputStream.flush();
}
} catch (Exception e) {
log.error(format("Failed to serve PDF document in servlet [%s]", guid), e);
} finally {
if (is != null) {
is.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
}

info++
28 Dec 2011, 12:30 AM
Thank u it s workin now &nbsp;<br>my mistake was a&nbsp;mistyping the servlet declaration in web.xml :)

DavidHoffer
28 Dec 2011, 5:58 AM
Good to hear!

Thanks,
-Dave