开发者

How to open 3 different PDF reports in a single window?

I am generating 3 different PDF reports with a servlet. There are 3 checkboxes in the JSP. If I click on all the 3 checkboxes, the PDF report will open in 3 windows by JavaScript. But I want them to be opened in a single window. How can I do this?

Here is the JavaScript:

for (i = 0; i < document.myform.FORMTYPE.le开发者_如何转开发ngth; i++) {
    if (document.myform.FORMTYPE[i].checked) {
        window.open('PDF_CNTRL_SERVLETS?FORMTYPE=' + document.myform.FORMTYPE[i].value + '&UNIQUEID1=' + CCNID + '&UNIQUEID2=' + arrestID);
    }         
}     


You need to collect the checked values in a single query string and then open only one window.

var formtypes = [];

for (i = 0; i < document.myform.FORMTYPE.length; i++) {
    if (document.myform.FORMTYPE[i].checked) {
        formtypes.push(document.myform.FORMTYPE[i].value); // Consider encodeURIComponent() the value whenever it is not guaranteed to contain only ASCII chars.
    }
}

if (formtypes.length) {
    window.open('PDF_CNTRL_SERVLETS?FORMTYPE=' + formtypes.join('&FORMTYPE=') + '&UNIQUEID1=' + CCNID + '&UNIQUEID2=' + arrestID);
}

In the servlet you can get all checked values by

String[] formtypes = request.getParameterValues("FORMTYPE");
// ...

Then you can let iText merge and output those 3 reports into a single response.


Here is some code I wrote to merge PDFs using Apache PDFBox.
Just loads the PDFs on the server into Streams or byte[] and call the correct method below.
The code runs on the server, all you need to do is load it into a new Window or Frame.

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.util.PDFMergerUtility;

public class PDFMerger
{
    /**
     * Given a List of byte[] which contain the PDFs will return a new PDF 
     * with the input PDFs appended one after the other
     * @param inputData
     * @return
     * @throws IOException
     */
    public static byte[] mergePDFData(List<byte[]> inputData) throws IOException 
    {
        byte[] retData = new byte[0];

        if(inputData != null && !inputData.isEmpty())
        {
            List<InputStream> inputs = new ArrayList<InputStream>();
            ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

            try
            {
                for(byte[] tmp : inputData)
                {
                    if(tmp != null && tmp.length > 0)
                    {
                        ByteArrayInputStream bis = new ByteArrayInputStream(tmp);
                        inputs.add(bis);
                    }
                }

                mergePDFStreams(bos,inputs);
                retData = bos.toByteArray();
            }
            finally
            {
                // close the streams
                if(bos != null) bos.close();
                if(inputs != null && inputs.size() > 0)
                {
                    for(InputStream is : inputs)
                    {
                        if(is != null) is.close();
                    }
                }
            }
        }

        return retData;
    }

    /**
     * Given a List of InputStreams containing PDFs will concatenate the PDFs to 
     * the output stream
     * @param oStream
     * @param inputStreams
     * @throws IOException
     */
    public static void mergePDFStreams(OutputStream oStream, List<InputStream> inputStreams ) throws IOException
    {
        if(oStream != null && inputStreams != null && !inputStreams.isEmpty())
        {
            PDFMergerUtility mergerUtility = new PDFMergerUtility();
            mergerUtility.addSources(inputStreams);
            mergerUtility.setDestinationStream(oStream);

            try
            {
                mergerUtility.mergeDocuments();
            }
            catch(COSVisitorException cosve)
            {
                throw new IOException(cosve);
            }
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜