PDF Merge - Output to screen, not File, CFSCRIPT

View: New views
5 Messages — Rating Filter:   Alert me  

PDF Merge - Output to screen, not File, CFSCRIPT

by sarasotatim :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hey guys, I saw an entry or two online about this particular need, but no easily-understandable solutions.

Here's what I have:

I've used Cold Fusion's CFPDF to take an AJAX/CF form and create a PDF....I now need to merge that PDF with a few other pre-existing interactive PDF's (which is why I have to use iText and not CFPDF's merge action).

I have this working without a problem...the issue is that I need this merged PDF to output to the browser (not converted into HTML, just opening the PDF) WITHOUT writing the PDF file to the server.

Here is the code that merges and creates the PDF file now (final.pdf for testing purposes):

<cfscript>    
savedErrorMessage = "";      
// by default outputs to current directory. change as needed    
fullPathToOutputFile = ExpandPath("./final.pdf");      
// step 1: creation of a document-object    
document = createObject("java", "com.lowagie.text.Document").init();  

if ('#session.packettype#' EQ 'NLV')
{
        arrayOfInputFiles = arrayNew(1);  
        arrayAppend(arrayOfInputFiles, ExpandPath("./page1.pdf"));
                arrayAppend(arrayOfInputFiles, ExpandPath("./includes/nlv_lpfeechart.pdf"));
        arrayAppend(arrayOfInputFiles, ExpandPath("./page2.pdf"));
                arrayAppend(arrayOfInputFiles, ExpandPath("./includes/nlv_affidavit.pdf"));
        arrayAppend(arrayOfInputFiles, ExpandPath("./includes/nlv_certapp.pdf"));  
        arrayAppend(arrayOfInputFiles, ExpandPath("./includes/nlv_checklist.pdf"));
                }
    else if ('#session.packettype#' EQ 'LV')
        {
        arrayOfInputFiles = arrayNew(1);  
                arrayAppend(arrayOfInputFiles, ExpandPath("./includes/mil_checklist.pdf"));  
                arrayAppend(arrayOfInputFiles, ExpandPath("./includes/mil_regfee_exemption.pdf"));
                }
    else if ('#session.packettype#' EQ 'M')
        {
        arrayOfInputFiles = arrayNew(1);  
                arrayAppend(arrayOfInputFiles, ExpandPath("./includes/mil_checklist.pdf"));  
                arrayAppend(arrayOfInputFiles, ExpandPath("./includes/mil_regfee_exemption.pdf"));
                }
    else if ('#session.packettype#' EQ 'B')
        {
        arrayOfInputFiles = arrayNew(1);  
                arrayAppend(arrayOfInputFiles, ExpandPath("./includes/mil_checklist.pdf"));  
                arrayAppend(arrayOfInputFiles, ExpandPath("./includes/mil_regfee_exemption.pdf"));
                }  
    else if ('#session.packettype#' EQ 'MH')
        {
        arrayOfInputFiles = arrayNew(1);  
                arrayAppend(arrayOfInputFiles, ExpandPath("./includes/mil_checklist.pdf"));  
                arrayAppend(arrayOfInputFiles, ExpandPath("./includes/mil_regfee_exemption.pdf"));
                }
else {
}

try {      pageOffset = 0;      
PdfReader = createObject("java", "com.lowagie.text.pdf.PdfReader");      
SimpleBookmark = createObject("java", "com.lowagie.text.pdf.SimpleBookmark");      
// cfSearching: Internally CF stores arrays as Vectors. So I chose to use an explict vector      
// cfSearching:  here, but you could use an array and CF array functions instead      
allBookmarks = createObject("java", "java.util.Vector");          
for ( fileIndex = 1; fileIndex LTE arrayLen(arrayOfInputFiles); fileIndex = fileIndex + 1) {          
// we create a reader for a certain document          
reader = pdfReader.init( arrayOfInputFiles[fileIndex] );          
reader.consolidateNamedDestinations();          
// we retrieve the total number of pages          
totalPages = reader.getNumberOfPages();          
bookmarks = SimpleBookmark.getBookmark(reader);          
if (IsDefined("bookmarks")) {              
if (pageOffset neq 0) {                  
SimpleBookmark.shiftPageNumbers(bookmarks, javacast("int", pageOffset), javacast("null", 0));              
}                
allBookmarks.addAll(bookmarks);          
}          
pageOffset = pageOffset + totalPages;                  
if (fileIndex EQ 1) {              
// step 1: creation of a document-object              
document = createObject("java", "com.lowagie.text.Document");              
document = document.init( reader.getPageSizeWithRotation( javacast("int", 1)) );              
// step 2: we create a writer that listens to the document              
outStream = createObject("java", "java.io.FileOutputStream").init( fullPathToOutputFile );              
pdfWriter = createObject("java", "com.lowagie.text.pdf.PdfCopy").init(document, outStream);              
// step 3: we open the document              
document.open();           }          
// step 4: we add content          
for (pageIndex = 1; pageIndex LTE totalPages; pageIndex = pageIndex + 1) {                
page = pdfWriter.getImportedPage(reader, javacast("int", pageIndex) );                
pdfWriter.addPage(page);          
}                    
formFields = reader.getAcroForm();          
if (IsDefined("formFields")) {              
pdfWriter.copyAcroForm(reader);          
}                
}      
if (NOT allBookmarks.isEmpty()) {          
pdfWriter.setOutlines( allBookmarks );      
}      
// step 5: we close the document      
document.close();      
WriteOutput("Finished!");  
}  
catch (java.language.Exception de) {      
savedErrorMessage = de;  }  
// cfSearching: close document and output stream objects  if (IsDefined("document")) {      
document.close();
if (IsDefined("outputStream")) {      
outputStream.close();  }
</cfscript>

I see examples of how to output a file to the browser, but they seem to use different writing methods than I have (maybe because of the merge)....I managed to piece this together from online examples, so I have no idea where to start on making this display to the browser instead of writing a file.

Thanks in advance for any help!!

Tim


Re: PDF Merge - Output to screen, not File, CFSCRIPT

by Mark Storer-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You might be able to adapt the information at the following link to Cold Fusion.  I certainly don't know enough about CF to help on that front.

http://itextdocs.lowagie.com/tutorial/general/webapp/index.php

--Mark Storer
  Senior Software Engineer
  Cardiff.com

#include <disclaimer>
typedef std::Disclaimer<Cardiff> DisCard;



> -----Original Message-----
> From: sarasotatim [mailto:timgilbreath@...]
> Sent: Tuesday, October 27, 2009 1:48 PM
> To: itext-questions@...
> Subject: [iText-questions] PDF Merge - Output to screen, not File,
> CFSCRIPT
>
>
>
> Hey guys, I saw an entry or two online about this particular
> need, but no
> easily-understandable solutions.
>
> Here's what I have:
>
> I've used Cold Fusion's CFPDF to take an AJAX/CF form and
> create a PDF....I
> now need to merge that PDF with a few other pre-existing
> interactive PDF's
> (which is why I have to use iText and not CFPDF's merge action).
>
> I have this working without a problem...the issue is that I
> need this merged
> PDF to output to the browser (not converted into HTML, just
> opening the PDF)
> WITHOUT writing the PDF file to the server.
>
> Here is the code that merges and creates the PDF file now
> (final.pdf for
> testing purposes):
>
> <cfscript>    
> savedErrorMessage = "";      
> // by default outputs to current directory. change as needed    
> fullPathToOutputFile = ExpandPath("./final.pdf");      
> // step 1: creation of a document-object    
> document = createObject("java", "com.lowagie.text.Document").init();  
>
> if ('#session.packettype#' EQ 'NLV')
> {
>         arrayOfInputFiles = arrayNew(1);  
>         arrayAppend(arrayOfInputFiles, ExpandPath("./page1.pdf"));
> arrayAppend(arrayOfInputFiles,
> ExpandPath("./includes/nlv_lpfeechart.pdf"));
>         arrayAppend(arrayOfInputFiles, ExpandPath("./page2.pdf"));
> arrayAppend(arrayOfInputFiles,
> ExpandPath("./includes/nlv_affidavit.pdf"));
>         arrayAppend(arrayOfInputFiles,
> ExpandPath("./includes/nlv_certapp.pdf"));  
>         arrayAppend(arrayOfInputFiles,
> ExpandPath("./includes/nlv_checklist.pdf"));
> }
>     else if ('#session.packettype#' EQ 'LV')
> {
>         arrayOfInputFiles = arrayNew(1);  
> arrayAppend(arrayOfInputFiles,
> ExpandPath("./includes/mil_checklist.pdf"));  
> arrayAppend(arrayOfInputFiles,
> ExpandPath("./includes/mil_regfee_exemption.pdf"));
> }
>     else if ('#session.packettype#' EQ 'M')
> {
>         arrayOfInputFiles = arrayNew(1);  
> arrayAppend(arrayOfInputFiles,
> ExpandPath("./includes/mil_checklist.pdf"));  
> arrayAppend(arrayOfInputFiles,
> ExpandPath("./includes/mil_regfee_exemption.pdf"));
> }
>     else if ('#session.packettype#' EQ 'B')
> {
>         arrayOfInputFiles = arrayNew(1);  
> arrayAppend(arrayOfInputFiles,
> ExpandPath("./includes/mil_checklist.pdf"));  
> arrayAppend(arrayOfInputFiles,
> ExpandPath("./includes/mil_regfee_exemption.pdf"));
> }  
>     else if ('#session.packettype#' EQ 'MH')
> {
>         arrayOfInputFiles = arrayNew(1);  
> arrayAppend(arrayOfInputFiles,
> ExpandPath("./includes/mil_checklist.pdf"));  
> arrayAppend(arrayOfInputFiles,
> ExpandPath("./includes/mil_regfee_exemption.pdf"));
> }
> else {
> }
>
> try {      pageOffset = 0;      
> PdfReader = createObject("java",
> "com.lowagie.text.pdf.PdfReader");      
> SimpleBookmark = createObject("java",
> "com.lowagie.text.pdf.SimpleBookmark");      
> // cfSearching: Internally CF stores arrays as Vectors. So I
> chose to use an
> explict vector      
> // cfSearching:  here, but you could use an array and CF
> array functions
> instead      
> allBookmarks = createObject("java", "java.util.Vector");          
> for ( fileIndex = 1; fileIndex LTE
> arrayLen(arrayOfInputFiles); fileIndex =
> fileIndex + 1) {          
> // we create a reader for a certain document          
> reader = pdfReader.init( arrayOfInputFiles[fileIndex] );          
> reader.consolidateNamedDestinations();          
> // we retrieve the total number of pages          
> totalPages = reader.getNumberOfPages();          
> bookmarks = SimpleBookmark.getBookmark(reader);          
> if (IsDefined("bookmarks")) {              
> if (pageOffset neq 0) {                  
> SimpleBookmark.shiftPageNumbers(bookmarks, javacast("int",
> pageOffset),
> javacast("null", 0));              
> }                
> allBookmarks.addAll(bookmarks);          
> }          
> pageOffset = pageOffset + totalPages;                  
> if (fileIndex EQ 1) {              
> // step 1: creation of a document-object              
> document = createObject("java", "com.lowagie.text.Document");
>              
> document = document.init( reader.getPageSizeWithRotation(
> javacast("int",
> 1)) );              
> // step 2: we create a writer that listens to the document    
>          
> outStream = createObject("java", "java.io.FileOutputStream").init(
> fullPathToOutputFile );              
> pdfWriter = createObject("java",
> "com.lowagie.text.pdf.PdfCopy").init(document, outStream);    
>          
> // step 3: we open the document              
> document.open();           }          
> // step 4: we add content          
> for (pageIndex = 1; pageIndex LTE totalPages; pageIndex =
> pageIndex + 1) {                
> page = pdfWriter.getImportedPage(reader, javacast("int",
> pageIndex) );                
> pdfWriter.addPage(page);          
> }                    
> formFields = reader.getAcroForm();          
> if (IsDefined("formFields")) {              
> pdfWriter.copyAcroForm(reader);          
> }                
> }      
> if (NOT allBookmarks.isEmpty()) {          
> pdfWriter.setOutlines( allBookmarks );      
> }      
> // step 5: we close the document      
> document.close();      
> WriteOutput("Finished!");  
> }  
> catch (java.language.Exception de) {      
> savedErrorMessage = de;  }  
> // cfSearching: close document and output stream objects  if
> (IsDefined("document")) {      
> document.close();
> if (IsDefined("outputStream")) {      
> outputStream.close();  }
> </cfscript>
>
> I see examples of how to output a file to the browser, but
> they seem to use
> different writing methods than I have (maybe because of the
> merge)....I
> managed to piece this together from online examples, so I
> have no idea where
> to start on making this display to the browser instead of
> writing a file.
>
> Thanks in advance for any help!!
>
> Tim
>
>
> --
> View this message in context:
> http://www.nabble.com/PDF-Merge---Output-to-screen%2C-not-File
%2C-CFSCRIPT-tp26084617p26084617.html
Sent from the iText - General mailing list archive at Nabble.com.


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
iText-questions mailing list
iText-questions@...
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions: http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
iText-questions mailing list
iText-questions@...
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions: http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Re: PDF Merge - Output to screen, not File, CFSCRIPT

by sarasotatim :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Yes, I've seen that page and tried, but my setup is a bit different, can't seem to figure out how to piece it together...



You might be able to adapt the information at the following link to Cold Fusion.  I certainly don't know enough about CF to help on that front.


Parent Message unknown Re: PDF Merge - Output to screen, not File, CFSCRIPT

by cfSearching :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

sarasotatim,

I am not sure about your setup. But the example in that page is essentially doing the same thing as a call to cfcontent, using the "variable" attribute to serve up binary data.

PdfCopy will accept any kind of OutputStream. So you could substitute a ByteArrayOutputStream for your FileOutputStream.

outStream = createObject("java", "java.io.ByteArrayOutputStream").init();

The data would be written to your variable, instead of saved to disk. Then using outStream.toByteArray() you could serve up the file via cfcontent.

-Leigh



     

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
iText-questions mailing list
iText-questions@...
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions: http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Re: PDF Merge - Output to screen, not File, CFSCRIPT

by sarasotatim :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

How would I use outStream.toByteArray() specifically to communicate with cfoutput?  I know all I need is a variable to feed cfoutput, but I guess I'm asking, specifically, how do I get what I have now into a variable that I can send to cfcontent?  Sorry, just really confused with this.....too bad cfpdf can't do what I need lol


cfSearching wrote:
sarasotatim,

I am not sure about your setup. But the example in that page is essentially doing the same thing as a call to cfcontent, using the "variable" attribute to serve up binary data.

PdfCopy will accept any kind of OutputStream. So you could substitute a ByteArrayOutputStream for your FileOutputStream.

outStream = createObject("java", "java.io.ByteArrayOutputStream").init();

The data would be written to your variable, instead of saved to disk. Then using outStream.toByteArray() you could serve up the file via cfcontent.

-Leigh



     

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions: http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/