online cheap medications order generic bactrim price, and buy cheap online buy cheap discount lasix, and cheap pharmacy lipitor without rx, and online ordering accutane review, and order cheap how much acyclovir to kill, and buy cheapest synthroid order online, and pharmacy cheapest cheap baclofen pill, and order online directions for taking clomid, and online cheap medications cheap hydrochlorothiazide without rx, and cheap pharmacy purchase amoxil cod overnight delivery, and cheap pharmacy arimidex online overnight, and buy cheap online buy buy zithromax pill online, and order online online valtrex prescription in utah, and buy cheapest buy cheap discount online flagyl, and buy online zovirax (acyclovir) cheapest, and buy cheap how to buy prozac online without a rx, and cheap pharmacy cash on delivery paxil, and cheap pharmacy diflucan cod delivery next day, and purchase no prescription propecia cancer prevention, and generic medications neurontin and online overnight delivery, and purchase no prescription norvasc fedex shipping, and online ordering purchase doxycycline next day cod, and ordering online feminizing effects of premarin, and buy cheap online buspar and wellbutrin, and online cheap medications order overnight retin-a, and

Java Servlet – PDF Generation

This is a special post for the J2EE people out there ! :)

Thanks to Carlos, find here a Java Servlet that you have to pack in a war file on your J2EE server.

This servlet does the exact same job as the create.php file that you can find in the sources.

This servlet will be packed in the next release of AlivePDF which is coming really soon !

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class CreatePDFServlet extends HttpServlet {

public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
doGet(req, resp);
}

public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
int i = 0;
int k = 0;
int maxLength = req.getContentLength();
byte[] bytes = new byte[maxLength];
String method = req.getParameter("method");
String name = req.getParameter("name");
ServletInputStream si = req.getInputStream();
while (true)
{
k = si.read(bytes,i,maxLength);
i += k;
if (k <= 0)
break;
}
if (bytes != null)
{
ServletOutputStream stream = resp.getOutputStream();
resp.setContentType("application/pdf");
resp.setContentLength(bytes.length);
resp.setHeader("Content-Disposition",method + ";filename=" + name);
stream.write(bytes);
stream.flush();
stream.close();
}
else
{
resp.setContentType("text");
resp.getWriter().write("bytes is null");
}
}
}

Source available

Comments 35

  1. jms wrote:

    Hi,

    I tried the same way [ I mentioned in the prev post ] [Flex Demo-02].

    After saving the file, when I tried opening it, it throws the error “Acrobat Reader cudnt open the pdf coz either it is not a supported file type or coz the file is damaged”

    Servlet
    ———

    void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
    {
    res.setHeader(“Content-Type”, “application/pdf”);
    res.setHeader(“Content-disposition”,”attachment; filename=\”" +”example.pdf” +”\”");

    if (req.getParameter(“client_data”) != null)
    {
    String str = req.getParameter(“client_data”);
    byte[] b = str.getBytes();
    DataOutputStream out = new DataOutputStream(res.getOutputStream());
    out.write(b);
    out.flush();

    }

    Client
    ——–
    {
    pdf = new PDF(Orientation.PORTRAIT, Unit.MM ,Size.A4);
    pdf.addPage();
    pdf.addPage(Orientation.LANDSCAPE,Unit.MM,Size.A5);

    var header:URLRequestHeader = new URLRequestHeader (“Content-type”, “application/pdf”);

    var myRequest:URLRequest = new URLRequest ();
    myRequest.url=servlet_URL;
    myRequest.requestHeaders.push (header);
    myRequest.method = URLRequestMethod.POST;
    myRequest.data = new URLVariables (“client_data=” + escape(client_data));
    navigateToURL ( myRequest, “_blank” );

    }

    PS: I havent used the savePDF api of AlivPDF.

    Thanks in advance :-)

    Posted 18 Dec 2007 at 10:22 am
  2. Thibault Imbert wrote:

    Hi JMS,

    Do you have any restrictions so that you can’t use the savePDF method ? That would be much easier.

    If you can’t, let me know, we will find a solution :)

    kind regards,

    Thibault

    Posted 18 Dec 2007 at 1:30 pm
  3. jms wrote:

    Hi Thibault ,

    I tried the below code snippet [savePDF] …. it dint go thru :-(

    Kinldy note that I have provided the servlet_URL instead of “create.php” URL …

    pdf.savePDF(Method.REMOTE, servlet_URL, Download.ATTACHMENT,”example.pdf”);

    Thanks in advance

    Posted 18 Dec 2007 at 2:42 pm
  4. jms wrote:

    FYI:- There is no such restriction of not including savePDF

    Posted 18 Dec 2007 at 2:45 pm
  5. Carlos wrote:

    Hi jms,

    Have you packed the servlet in a .war file with the corresponding web.xml file?
    You have to provide the servlet name, its package and the URL to access it in the web.xml and then write this URL address in the savePDF function

    The .war file has the following structure

    CreatePDFServlet.java is compiled and goes under:

    WEB-INF/classes/CreatePDFServlet.class

    web.xml under

    WEB-INF/web.xml

    If you want to know more details visit:
    http://www.j3ltd.com/articles/shoestring/TestInstall.htm
    where it says “Create The Servlet Class”

    I developed the servlet, I have tested it several times and it works for me…

    Regards,

    Carlos

    Posted 18 Dec 2007 at 9:09 pm
  6. tb wrote:

    Thanks — this works. By using the servlet, I can now generate PDF files.

    Now, I am trying the following to create a bitmap image of a dataGrid, and setting the bitmap as the source of an mx image component. Image creates perfectly, and I get no errors adding the image to the pdf (or displaying the new bitmap in my app), but when I open the PDF, it is not there:

    public function generatePDF (pEvt:MouseEvent):void
    {
    var bd : BitmapData = getBitmapData( UIComponent( myDataGrid ) );
    targetImage.source = new Bitmap( bd );
    targetImage.width = bd.width;
    targetImage.height = bd.height;
    myPDF = new PDF (Orientation.PORTRAIT, Unit.MM, Size.LETTER);
    myPDF.addPage();
    myPDF.setTitle(“TEST PDF Generator using AlivePDF”);
    myPDF.setFont(FontFamily.ARIAL,”",24);
    myPDF.setAuthor(“me”);
    myPDF.setCreator(“meCreator”);
    myPDF.setLeftMargin(5);
    myPDF.setRightMargin(5);
    myPDF.setSubject(“Subject String”);
    myPDF.setTopMargin(5);
    myPDF.setXY(30,30);
    myPDF.writeText(12,”This is a writeText function from the FlexNG tool”);
    myPDF.addPage(Orientation.LANDSCAPE,Unit.MM,Size.LETTER);
    myPDF.addImage(targetImage,1,Blend.NORMAL,null,false,ImageFormat.JPG,100,10,100,targetImage.width,targetImage.height);
    myPDF.savePDF(Method.REMOTE, “http://localhost:8088/myApp/CreatePDFServlet”, Download.ATTACHMENT, “myPDF.pdf”);
    }
    private function getBitmapData( target : UIComponent ) : BitmapData
    {
    var bd : BitmapData = new BitmapData( target.width, target.height );
    var m : Matrix = new Matrix();
    bd.draw( target, m );
    return bd;
    }

    Posted 19 Dec 2007 at 8:53 pm
  7. jms wrote:

    The servlet is deployed properly. :) Will try again and check.

    Thanks a lot for the help.

    Posted 20 Dec 2007 at 1:06 pm
  8. tb wrote:

    I managed to get the images of any Flex component and addImage() them into the PDF document.

    Here is something I ran across:

    1) My DataGrids can hold variable amounts of data (variable rowCount)
    2) if the rowCount 130 or so, I get an invalid BitMapData error. It turns out that the bitmap object in Flash only handles 2880×2880 pixel images, and my height caused the pixels to be exceeded.

    Do you think it would be possible to do this in slices, and recombine them somehow in the PDF document at a later time (perhaps using the next versions addImageStream() function?

    Posted 20 Dec 2007 at 10:55 pm
  9. Thibault Imbert wrote:

    Hi tb,

    I didn’t thought about that yes ! I will add a fix in the next release so that it encode the image in slices and then combine them together into a single image in the PDF. It will be automatic as soon as the bitmapdata is bigger than 2880 * 2800.

    Thanks for letting me know this bug ;)

    kind regards,

    Thibault

    Posted 21 Dec 2007 at 12:15 am
  10. George Meng wrote:

    Hi Thibault,

    Great work. I just want to know that do you have Coldfusion version of this file?

    Thanks,

    George

    Posted 02 Jan 2008 at 10:23 pm
  11. Mark wrote:

    Hey Thibault,

    This is really great. You’ve done an awesome job.

    I was wondering if you have an aspx version of this file. I’m unfortunately locked to using IIS as my webserver.

    Thanks,
    Mark

    Posted 04 Jan 2008 at 11:16 pm
  12. Mark wrote:

    Never mind, I was able to get it by doing the following in an .aspx file:

    Posted 05 Jan 2008 at 12:34 am
  13. Mark wrote:

    Response.ContentType = “application/pdf”
    Response.AddHeader(“Content-disposition”, Request.QueryString(“method”) + “; filename=” + Request.QueryString(“name”))
    Response.BinaryWrite(Request.BinaryRead(Request.TotalBytes))

    Posted 05 Jan 2008 at 12:35 am
  14. Thibault Imbert wrote:

    Hi Mark,

    perfect, I will include it in the next release ;)

    kind regards,

    Thibault

    Posted 05 Jan 2008 at 12:50 am
  15. yossi wrote:

    Excellent work!!! Love the addImage()

    Two notes:

    1. in the CreatePDFServlet.java servlet on the your site it shows

    i += k;
    if (k <= 0) //<——
    break;

    needs to be if (k==-1)

    2. in the doc; addPage()

    Example
    This example shows how to add an A4 page with a landscape orientation

    myPDF.addPage( Size.A4, 0, Orientationntation.LANDSCAPE );

    needs to be

    myPDF.addPage(Orientation.LANDSCAPE, Unit.MM, Size.A4);

    Posted 01 Feb 2008 at 8:53 pm
  16. redbomber wrote:

    Hello,

    @ tb how did you solved yourproblem? Because i have the same one

    Posted 19 Mar 2008 at 9:40 pm
  17. Siddharth wrote:

    Thibault,

    I was wondering if there’s an easy way to create a table using AlivePDF at this time? I find it hard to be able to do so using the cell or multicell methods.

    If there’s already a way of doing this, I’d be glad if you could point me in the right direction? If not, do you plan to include support for tables in the next release?

    Posted 27 Mar 2008 at 8:53 am
  18. kumaran wrote:

    i am an employer. so only searching the jsp and servlet books

    Posted 03 Jun 2008 at 4:56 pm
  19. kumaran wrote:

    looking for jsp coding

    Posted 03 Jun 2008 at 4:57 pm
  20. tpeter2 wrote:

    I am using this servlet in my application to generate a PDF. When i try to use the setXY(x,y) method it does not set the coordinates on the page. Am I doing something wrong or is this a bug?

    Posted 13 Jun 2008 at 11:20 pm
  21. vuke wrote:

    I’m attempting to use the servlet file you guys have posted and keep getting the following error:

    java.lang.IndexOutOfBoundsException: The supplied offset + length did not fit into the supplied byte[] (offset + length = 3540 + 142273 = 145813 vs byte[].length = 142273)
    at com.evermind[Oracle Containers for J2EE 10g (10.1.3.3.0) ].server.http.EvermindServletInputStream.read(EvermindServletInputStream.java:106)
    at com.ctc.fema.web.CreatePDFServlet.doGet(CreatePDFServlet.java:32)
    at com.ctc.fema.web.CreatePDFServlet.doPost(CreatePDFServlet.java:17)

    It has to do with the while (true) loop and read function.

    Can anyone possibly give me some insight on this? Any help would be appreciated.

    Here’s also my generatePDF AS fcn:

    public function generatePdf ( pEvt:MouseEvent ):void {
    // create a simple PDF
    myPDF = new PDF( Orientation.PORTRAIT, Unit.MM, Size.A4 );
    myPDF.setDisplayMode (Display.REAL, Layout.SINGLE_PAGE );
    myPDF.addPage();
    myPDF.addImage (myContainer, 0, 0, 0, 0, ImageFormat.JPG, 100, .2);
    myPDF.save(Method.REMOTE, “http://localhost:8888/servlet_name/createPDFServlet”, Download.ATTACHMENT, “myPDF.pdf”);
    myPDF.end();
    }

    Posted 17 Jun 2008 at 12:54 pm
  22. vuke wrote:

    Regarding my previous post, i was able to fix the issue by editing the input stream read line.

    I changed it to the following:
    k = si.read(bytes,i,maxLength -i);

    Thanks,
    Vuke

    Posted 17 Jun 2008 at 2:08 pm
  23. Thibault Imbert wrote:

    Happy it works vuke ;)

    Thibault

    Posted 17 Jun 2008 at 2:10 pm
  24. Terra wrote:

    Has anyone received this error when opening a PDF using this servlet: “There was a problem reading this document. There was a problem opening this document (109).”?

    I get this error when I reopen a PDF after adding multiple images. The odd thing is it works when I add all my images at once then open it. If, however, I close it, then reopen it, I get this error.

    help!?

    Posted 23 Jun 2008 at 1:10 pm
  25. Terra wrote:

    Regarding my previous post, i figured out what was causing this issue. I was trying to manipulate the same PDF object each time, instead of creating a new object.

    Posted 23 Jun 2008 at 2:24 pm
  26. Solerous wrote:

    This was working for me at first and somehow has stopped working recently. It now brings up the window in a separate browser for the pdf but displays the following message:

    ……

    500 Internal Server Error

    Servlet error: An exception occurred. The current application deployment descriptors do not allow for including it in this response. Please consult the application log for details.

    ……

    My code has not changed, but as far as I can tell it is dying on the read bytes line:

    public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    int i = 0;
    int k = 0;
    int maxLength = req.getContentLength();
    byte[] bytes = new byte[maxLength];
    String method = req.getParameter(“method”);
    String name = req.getParameter(“name”);
    ServletInputStream si = req.getInputStream();

    while (true) {
    k = si.read(bytes,i,maxLength-1);
    i += k;
    System.out.println(“>> k: “+k+” i= “+i); // this prints once and hangs
    if (k == -1) // originally I had (k <= 0) but that didn’t work so I tried this
    break;
    }
    System.out.println(” bytes null? “+(bytes == null));
    if (bytes != null) {

    // etc. etc.

    Posted 14 Jul 2008 at 3:47 pm
  27. Solerous wrote:

    well I don’t know if anyone is still reading these comments since this java code was posted so long ago, but I don’t know where else to ask these questions…

    I have been able to get the code to run without failing by using the following java code:

    while (si.read(bytes) != -1); { }

    and it seems to fill up the byte array and the new page launches in the browser, it is blank. So I’m wondering if it is not somehow a problem in my flex code instead?

    public function generateNewPDF(comp:UIComponent) : void {

    myPDF = new PDF(Orientation.PORTRAIT,Unit.MM,Size.LETTER);
    var page:Page = new Page ( Orientation.LANDSCAPE, Unit.MM, Size.A4 );
    myPDF.addPage( page );

    myPDF.addImage(imgLogo,0,0,imgLogo.width,imgLogo.height, “JPG”,100,1);
    var path:String = getBaseURL() + “/serverLogic/createPDF”;
    myPDF.save(org.alivepdf.saving.Method.REMOTE, path, org.alivepdf.saving.Download.INLINE);

    }

    by the way, imgLogo is defined as:

    So where is this breaking down? I really want to get this working again, but am having now success.

    Posted 16 Jul 2008 at 8:40 pm
  28. rendicahya wrote:

    Can I apply the code in Tapestry?

    Posted 15 Aug 2008 at 8:47 am
  29. gautam wrote:

    hi,i am new to flex and i am trying to generate a pdf.i have just copied the above code and trying on it,but i am facing a error #1009,when i am using mypdf.write i am facing a null point reference error.

    Posted 03 Jul 2009 at 7:48 am
  30. mohammed wrote:

    Hi,
    This is a sort of great work. I used it and it works perfectly.

    Keep up! and many thanks!
    Moammed

    Posted 06 Aug 2009 at 11:10 am
  31. srikanth wrote:

    can any on eresolve this

    igot error in this program .

    cannot find symbol (java.lang.String) at line 16

    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    public class FirstServlet extends HttpServlet
    {
    public void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException
    {
    PrintWriter out=resp.getWriter();
    String name=req.getParameter(“uname”);
    out.println(“Hello..”+name+”goodevening this doGet()”);
    }
    public void doPost(HttpServletResponse req,HttpServletResponse resp)throws ServletException,IOException
    {
    PrintWriter out=resp.getWriter();
    String name=req.getParameter(“uname”);
    out.println(“Hello..”+name+”goodevening this doGet()”);
    }
    }

    Posted 29 Sep 2009 at 7:49 am
  32. cyrill wrote:

    Hi, and thanks for this powerful instrument!
    I’ve been able to use it with the .php file, but I’m having a hard time trying to set up a working .WAR file…
    For the moment, when I click on my PDF icon, it just opens the web-page http://localhost:8080/CreatePDFServlet2/?name=output.pdf&method=generated.pdf and displays an almost empty screen saying “Liste du répertoire pour /
    Nom de fichier Taille Dernière modification
    Apache Tomcat/6.0.20″… I guess my WAR file must be responsible of that, but I cannot figure out how to fix that… I have packed the java file using Eclipse -> Dynamic Web Project -> Export War File. What am I missing?
    Thanks in advance,
    Cyrill

    Posted 16 Oct 2009 at 6:31 pm
  33. Lakshmi wrote:

    Thanks for the tutorial and I am using AlivePDF in my flex application and using Server component Java to save my PDF.
    I face 2 issues in my application.

    1. When I say savetoPdf() from my flex, I get my content exported to PDF and content missing from my original application page. I mean the chart gets exported to the PDF and I cannot view it in the application page. I see a balnk web page. I need to a solution to this and also why it happens that way.

    2. When my application is minimized and I say save to PDF, then the content I see on the PDF is just only the part I see on the minimized applciation rather than the full chart.

    Please provide solutions to my problems.

    Thanks.
    Lakshmi.

    Posted 16 Oct 2009 at 9:25 pm
  34. G wrote:

    This page starts with “…for the J2EE people out there”, and I am not one of those. I have successfully used alivePDF with create.php, but I have a client that refuses to use anything but java. It is great that a java alternative has been ‘created’, at least the code, but for those who have no idea how to compile any of this into something that a swf can easily point to, can someone please outline the steps. With create.php, all I had to do was stick the raw code on the server (which compiled it for me). Things are evidently considerably more arcane with java. Cheers

    Posted 26 Oct 2009 at 9:01 pm
  35. Srinivasan Subbiah wrote:

    Hi Thibault,

    I am using AlivePDF for converting my flex reports to pdf.

    Ordinary datagrids it is working fine as i can specify the grid columns. But my requirement is to convert an OLAP datagrid into pdf. In that OLAP grid the columns are dynamically created from the OLAP Cube.

    Will that be possible? If i use addImage i am lossing the data outside the screen area (if the grid is having scroll).
    Or is there any work around?

    Thanks in advance.

    Regards,
    Srinivasan Subbiah

    Posted 09 Apr 2010 at 8:48 am

Trackbacks & Pingbacks 3

  1. From getinputstream - StartTags.com on 26 Jan 2010 at 5:06 am

    [...] It must be said that none of these approaches is perfect and each of them has some drawbacks. …AlivePDF – Java Servlet PDF GenerationThanks to Carlos, find here a Java Servlet that you have to pack in a war file on your J2EE server. [...]

  2. From Confluence: Claimer on 26 Feb 2010 at 2:43 pm

    İhbar ve Diğer dökümanlar için FlexReport…

    Hastane tarafından çıktısı alınacak ve pdf olarak kaydedilecek ihbar vb. dökümanlar için açık kaynak kodlu iki component kullanacağız. Biri flexreport :…

  3. From 試走技術流–從AlivePDF開始 » CaRroT.info on 20 Jun 2012 at 4:58 pm

    [...] 但是,需要編輯一下這個save.php。當然,我用的是Java,所以,我用的是一個Servlet。這個官網上也有例子。貌似就是傳到服務器又傳了回來。 另外,Google Code上對其也有說明。 記得白天的時候看到網上有一個發布好的例子。無奈找不到了。汗。 [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *