Save PDF in memory

Help
2013-05-01
2013-05-28
  • Tomas Bucki

    Tomas Bucki - 2013-05-01

    Hi all,
    thanks for the nice library in which I am new to it. I want to save < 2 GB PDf into memory, is it safe to use :

    ILocator loc = new ByteArrayLocator(null, "pdf");
    fontdoc.save(loc);
    loc.synch(); // perform flush and close ???
    @Cleanup
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    IOUtils.copy(loc.getInputStream(), os);
    byte result = os.toByteArray();

    Unfortunatelly I do not know the size of PDF so I cannot use ByteArrayLocator with the pre-alocated buffer. Or there is some other bettter way how to achieve serialisation of whole PDF to byte ?

    Thank you,
    Tomas

     
  • mtraut

    mtraut - 2013-05-02

    As another user reported earlier, the resize mechanics currently used are not very efficient with large documents. resizing is done in small chunks, leading to lots of copy operations. so it seems to be a good idea to create a (large) buffer upfront, maybe based on a rough estimation. If you have problems anyway, you may opt for changing the resize strategy in "RandomAccessByteArray".

    A "synch" operation is not needed - this is an interface describing the ability to check changes on physical storage (hot deploy).

    Also, you do not need to copy via a stream, "getBytes" will be more efficient.

     
  • Tomas Bucki

    Tomas Bucki - 2013-05-02

    Dear MTraut,

    thanks for the quick reply. I finally ended up with the following code:
    ByteArrayLocator loc = new ByteArrayLocator(null,
    PDFUtils.getid());
    fontdoc.save(loc);
    return loc.getBytes();
    The generated PDF is in my case in this function only one-paged and contains only one resource, as the business logic behind is such. Typical size is about 20 - 300 Kb so I do not worry :-)

    Once again many thanks for excellent library, I have tryed iText, PDFBox and JPod and for low level working with PDF's yours is the best one !

     

Log in to post a comment.