<script type="text/javascript" src="./js/pdfjs/pdfobject.js"></script> <body> <div id="pdfDiv" style="width:100%;height:620px !important;"></div> <script type="text/javascript"> PDFObject.embed("URL?parameter=name", "#pdfDiv"); </script> </body>
1: the front-end realizes the acquisition and display of PDF files through the pdfobject.js plug-in.
PDFObject.embed(URL, "#pdfDiv");
The URL can be a static resource file (such as. / staticFiles/name.pdf) or a Servlet path (such as getFileAction?name=name).
package com.kangping.service; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONArray; import com.itextpdf.text.Document; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfImportedPage; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfWriter; public class PrintMorePDF extends HttpServlet { @Override public void service(HttpServletRequest request, HttpServletResponse response) { try { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); PrintPDFs(request, response); } catch (Exception e) { e.printStackTrace(); } } public static void PrintPDFs(HttpServletRequest request, HttpServletResponse response) throws Exception { String fileNames = request.getParameter("fileNames"); if (null == fileNames) { response.getWriter().write("<script>alert('cannot find file');</script>"); return; } List<InputStream> streamOfPDFFiles = new ArrayList<InputStream>(); JSONArray jsonArr = new JSONArray(fileNames); if (jsonArr.length() < 1) { response.getWriter().write("<script>alert('cannot find file');</script>"); return; } String notExistFileNames = ""; for (int i = 0; i < jsonArr.length(); ++i) { File file = new File(jsonArr.getString(i)); if (!file.exists()) { notExistFileNames += jsonArr.getString(i); } } if (notExistFileNames.length() > 1) { notExistFileNames = notExistFileNames.substring(0, notExistFileNames.length() - 1); response.getWriter().write( "<script>alert('cannot find file:" + notExistFileNames + "');</script>"); return; } for (int i = 0; i < jsonArr.length(); ++i) { InputStream in = new FileInputStream(jsonArr.getString(i)); streamOfPDFFiles.add(in); } new PrintMorePDF().concatPDFs(streamOfPDFFiles, response .getOutputStream(), false); } public void concatPDFs(List<InputStream> streamOfPDFFiles, OutputStream outputStream, boolean paginate) { Document document = new Document(); try { List<InputStream> pdfs = streamOfPDFFiles; List<PdfReader> readers = new ArrayList<PdfReader>(); int totalPages = 0; Iterator<InputStream> iteratorPDFs = pdfs.iterator(); // Create Readers for the pdfs. while (iteratorPDFs.hasNext()) { InputStream pdf = iteratorPDFs.next(); PdfReader pdfReader = new PdfReader(pdf); readers.add(pdfReader); totalPages += pdfReader.getNumberOfPages(); } // Create a writer for the outputstream PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); PdfContentByte cb = writer.getDirectContent(); // Holds the PDF // data PdfImportedPage page; int currentPageNumber = 0; int pageOfCurrentReaderPDF = 0; Iterator<PdfReader> iteratorPDFReader = readers.iterator(); // Loop through the PDF files and add to the output. while (iteratorPDFReader.hasNext()) { PdfReader pdfReader = iteratorPDFReader.next(); // Create a new page in the target for each source page. while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) { document.newPage(); pageOfCurrentReaderPDF++; currentPageNumber++; page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF); cb.addTemplate(page, 0, 0); // Code for pagination. if (paginate) { cb.beginText(); cb.setFontAndSize(bf, 9); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "" + currentPageNumber + " of " + totalPages, 520, 5, 0); cb.endText(); } /* * pdfs.size() != 1 Add pages only when the total number of detected current PDF s is greater than 1 */ /* * If it is detected that the total number of pages of the current document is odd, a blank page is added to the previous page of the last page to make the document an even page document, and the document separation is realized during printing. */ /* if ((pageOfCurrentReaderPDF + 1) == pdfReader */ /* * If it is detected that the total number of pages of the current document is odd, a blank page is added to the last page to make the document an even page document, and document separation is realized during printing. */ if (pdfs.size() > 1 && pageOfCurrentReaderPDF == pdfReader.getNumberOfPages() && pdfReader.getNumberOfPages() % 2 == 1) { document.newPage(); document.add(new Paragraph(" ")); } } pageOfCurrentReaderPDF = 0; } outputStream.flush(); document.close(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (document.isOpen()) document.close(); try { if (outputStream != null) outputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } }
2: the jar package for document operation is used in the background. Due to the demand problem, this code will merge multiple PDF files into one PDF file.
The idea is to apply for a piece of memory, then get the static PDF file circularly, merge it, and send the file stream to the client through the Outputstream of the response.
3: front PDF plug-in and back jar package click here