Question on Java Servlet to open a PDF file using iText -
the code below grabs pdf file , displays in browser.
import java.io.bytearrayoutputstream; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.outputstream; import java.io.printwriter; import javax.servlet.servletconfig; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import com.lowagie.text.document; import com.lowagie.text.documentexception; import com.lowagie.text.pdf.pdfreader; import com.lowagie.text.pdf.pdfstamper; import com.lowagie.text.pdf.pdfstream; import com.lowagie.text.pdf.pdfwriter; public class welcomeservlet extends httpservlet { private static final string document_location = "h:\\testpdf.pdf"; // test pdf on pc @override public void init(servletconfig config) throws servletexception { super.init(config); } protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { bytearrayoutputstream baos = new bytearrayoutputstream(); pdfreader reader = new pdfreader(document_location); pdfstamper stamper = null; try { stamper = new pdfstamper(reader, baos); } catch (documentexception e) { e.printstacktrace(); } try { stamper.close(); } catch (documentexception e) { system.out.println("why making me this?"); e.printstacktrace(); } // set response headers response.setheader("expires", "0"); response.setheader("cache-control", "must-revalidate, post-check=0, pre-check=0"); response.setheader("pragma", "public"); response.setcontenttype("application/pdf"); response.setcontentlength(baos.size()); outputstream os = response.getoutputstream(); baos.writeto(os); os.flush(); os.close(); } }
if change code, doesn't return file. difference lines deal pdfstamper removed.
import java.io.bytearrayoutputstream; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.outputstream; import java.io.printwriter; import javax.servlet.servletconfig; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import com.lowagie.text.document; import com.lowagie.text.documentexception; import com.lowagie.text.pdf.pdfreader; import com.lowagie.text.pdf.pdfstamper; import com.lowagie.text.pdf.pdfstream; import com.lowagie.text.pdf.pdfwriter; public class welcomeservlet extends httpservlet { private static final string document_location = "h:\\testpdf.pdf"; // test pdf on pc @override public void init(servletconfig config) throws servletexception { super.init(config); } protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { bytearrayoutputstream baos = new bytearrayoutputstream(); pdfreader reader = new pdfreader(document_location); // set response headers response.setheader("expires", "0"); response.setheader("cache-control", "must-revalidate, post-check=0, pre-check=0"); response.setheader("pragma", "public"); response.setcontenttype("application/pdf"); response.setcontentlength(baos.size()); outputstream os = response.getoutputstream(); baos.writeto(os); os.flush(); os.close(); } }
why work pdfstamper? don't want stamping, want display pdf in browser. there i'm missing takes place of pdfstamper in itext???
when use pdfstamper
reading in file disk , writing baos
. when removed pdfstamper
, baos
never gets written to. of course, baos
empty, never returns anything.
edit: want (the pdfreader necessary if want modify pdf):
private static void copy(inputstream is, outputstream os) throws ioexception { byte buffer[] = new byte[8192]; int bytesread, i; while ((bytesread = is.read(buffer)) != -1) { os.write(buffer, 0, bytesread); } } protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { fileinputstream baos = new fileinputstream(document_location); // set response headers response.setheader("expires", "0"); response.setheader("cache-control", "must-revalidate, post-check=0, pre-check=0"); response.setheader("pragma", "public"); response.setcontenttype("application/pdf"); response.setcontentlength(new file(document_location).length()); outputstream os = response.getoutputstream(); copy(baos, os); os.flush(); os.close(); } }
Comments
Post a Comment