{oapdf_gg}
p> 2) to PDF files through the form of transport stream to the client's cache. The benefits of doing so will not be any left on the server "relics." p> p> i) directly through generate JSP pages p> p> p> <%@ p> page import = "java.io. *, java.awt.Color, com.lowagie.text .*, com.lowagie.text.pdf .*"%> p> <% p> response.setContentType ( "application/ pdf"); p> Document document = new Document (); p> ByteArrayOutputStream buffer = new ByteArrayOutputStream (); p> PdfWriter writer = PdfWriter.getInstance (document, buffer); p> document.open (); p> document.add (new Paragraph ( "Hello World ")); p> document.close (); p> DataOutput output = new DataOutputStream (response.getOutputStream ()); p> byte [] bytes = buffer.toByteArray (); p> response.setContentLength (bytes.length); p> for (int i = 0; i ( p>
output.writeByte (bytes [i]); p>
) p>
%> p>
p>
ii) through the Servlet to generate p>
p>
p>
import java.io. *; p>
import javax.servlet .*; p>
import javax.servlet.http .*; p>
import com.lowagie.text .*; p>
import com.lowagie.text.pdf .*; p>
public void doGet (HttpServletRequest request, HttpServletResponse response) p>
throws IOException, ServletException p>
( p>
Document document = new Document (PageSize.A4, 36,36,36,36); p>
ByteArrayOutputStream ba = new ByteArrayOutputStream (); p>
try p>
( p>
PdfWriter writer = PdfWriter.getInstance (document, ba); p>
document.open (); p>
document.add (new Paragraph ( "Hello World ")); p>
) p>
catch (DocumentException de) p>
( p>
de.printStackTrace (); p>
System.err.println ( "A Document error:" + de.getMessage ()); p>
) p>
document.close (); p>
response.setContentType ( "application/ pdf "); p>
response.setContentLength (ba.size ()); p>
ServletOutputStream out = response.getOutputStream (); p>
ba.writeTo (out); p>
out.flush (); p>
) p>
I used in the project is the second method. In this paper, the source code in my debugger tomcat4 above are adopted. Hope that we can bring convenience to everyone. p> |