java ee - Exception Reason while reading pdf in jsp -
i have wrote jsp servlet read pdf using itextpdf , end exception can tell me reason exception
page.jsp
<html> <%@page import="java.io.file"%> <%@page import="java.io.*"%> <%@page import="javax.servlet.*"%> <%@page import="com.itextpdf.text.image"%> <%@page import="com.itextpdf.text.document"%> <%@page import="com.itextpdf.text.documentexception"%> <%@page import="com.itextpdf.text.pdf.pdfreader"%> <%@page import="com.itextpdf.text.pdf.pdfimportedpage"%> <%@page import="com.itextpdf.text.pdf.pdfwriter"%> <%@page import="com.itextpdf.text.pdf.pdfcontentbyte"%> <% response.reset(); response.setcontenttype("application/pdf"); response.setheader("content-disposition", "inline;filename=saba_phbill.pdf"); file file = new file( "d:\\tnwrd_documents\\knowladge_base\\chapter_i.pdf"); string outputfile = "d:\\tnwrd_documents\\chapter_ii.pdf"; fileinputstream in = new fileinputstream(file); pdfreader reader = new pdfreader(in); document document = new document(); //pdfwriter writer = pdfwriter.getinstance(document, // new fileoutputstream(outputfile)); outputstream outputstream = new fileoutputstream(outputfile); pdfwriter writer = pdfwriter.getinstance(document, outputstream); document.open(); //pdfreader reader = new pdfreader(inputfile); pdfcontentbyte cb = writer.getdirectcontent(); int n = reader.getnumberofpages(); pdfimportedpage pages; // go through pages (int = 1; <= n; i++) { // page number 2 included pages = writer.getimportedpage(reader, i); image instance = image.getinstance(pages); document.add(instance); cb.addtemplate(pages, 0, 0); document.addheader("pdf", "pdf"); } document.close(); %> </html>
well, won't able display pdf in browser if enclose pdf file between <html>
, </html>
tags. that's illegal syntax.
furthermore, you're writing pdf fileoutputstream
. that's... odd. need send pdf bytes servletoutputstream.
this simple hello world servlet: hello. see outputstream used when creating pdfwriter
instance? response.getoutputstream()
servletoutputstream
instance.
of course, while works browsers, you'll encounter problems legacy browser versions. that's why there's example: pdfservlet
finally, you're jsp developer, remember jsp courses creating binary files jsp bad idea. developers write servlet create binary documents.