Mercurial > hg > digilib-old
changeset 594:85e465e6a642
fixes for PDF generation
author | robcast |
---|---|
date | Fri, 07 Jan 2011 17:47:24 +0100 |
parents | a667d2952aab |
children | e8668edcb880 bd276c765a5d cad6d3d5aeb3 |
files | servlet/src/digilib/pdf/PDFStreamWorker.java servlet/src/digilib/pdf/PDFTitlePage.java servlet/src/digilib/servlet/PDFCache.java servlet/src/digilib/servlet/Scaler.java |
diffstat | 4 files changed, 85 insertions(+), 140 deletions(-) [+] |
line wrap: on
line diff
--- a/servlet/src/digilib/pdf/PDFStreamWorker.java Thu Jan 06 20:51:58 2011 +0100 +++ b/servlet/src/digilib/pdf/PDFStreamWorker.java Fri Jan 07 17:47:24 2011 +0100 @@ -78,25 +78,24 @@ addTitlePage(doc); - logger.debug("- " + outstream + " doc.open()ed (" + logger.debug("PDF: " + outstream + " doc.open()ed (" + (System.currentTimeMillis() - start_time) + "ms)"); - start_time = System.currentTimeMillis(); NumRange pgs = job_info.getPages(); for (int p : pgs) { - logger.debug(" - adding Image " + p + " to " + outstream); + logger.debug("PDF: adding Image " + p + " to " + outstream); // create ImageJobInformation ImageJobDescription iji = ImageJobDescription.getInstance(job_info, job_info.getDlConfig()); iji.setValue("pn", p); addImage(doc, iji); - logger.debug(" - done adding Image " + p + " to " + outstream); + logger.debug("PDF: done adding Image " + p + " to " + outstream); } - logger.debug(" - done adding all Images to " + outstream); + logger.debug("PDF: done adding all Images to " + outstream); doc.close(); - logger.debug("- " + outstream + " doc.close() (" + logger.debug("PDF: " + outstream + " doc.close() (" + (System.currentTimeMillis() - start_time) + "ms)"); docwriter.close(); return outstream;
--- a/servlet/src/digilib/pdf/PDFTitlePage.java Thu Jan 06 20:51:58 2011 +0100 +++ b/servlet/src/digilib/pdf/PDFTitlePage.java Fri Jan 07 17:47:24 2011 +0100 @@ -1,5 +1,6 @@ package digilib.pdf; +import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; @@ -14,9 +15,8 @@ import com.itextpdf.text.Image; import com.itextpdf.text.Paragraph; - import digilib.io.DigilibInfoReader; -import digilib.io.DocuDirCache; +import digilib.io.FileOpException; import digilib.servlet.PDFCache; import digilib.servlet.PDFRequest; @@ -28,7 +28,6 @@ private PDFRequest job_info = null; private DigilibInfoReader info_reader= null; - private DocuDirCache dirCache = null; protected static Logger logger = Logger.getLogger("digilib.servlet"); @@ -38,12 +37,27 @@ */ public PDFTitlePage(PDFRequest pdfji){ job_info = pdfji; - dirCache = (DocuDirCache) job_info.getDlConfig().getValue("servlet.dir.cache"); + + // use MPIWG-style info.xml + info_reader = getInfoXmlReader(pdfji); + } - String fn = getBase(dirCache.getDirectory(pdfji.getImageJobInformation().getAsString("fn")).getDir().getPath()) + "presentation/info.xml"; - - info_reader = new DigilibInfoReader(fn); - } + /** + * @param pdfji + * @return + */ + protected DigilibInfoReader getInfoXmlReader(PDFRequest pdfji) { + try { + // try to load ../presentation/info.xml + File imgDir = pdfji.getImageJobInformation().getFileDirectory().getDir(); + File docDir = imgDir.getParentFile(); + File infoFn = new File(new File(docDir, "presentation"), "info.xml"); + return new DigilibInfoReader(infoFn.getAbsolutePath()); + } catch (FileOpException e) { + logger.warn("info.xml not found"); + } + return null; + } /** * generate iText-PDF-Contents for the title page @@ -59,7 +73,6 @@ content.add(Chunk.NEWLINE); } - // add logo content.add(getLogo()); content.add(Chunk.NEWLINE); @@ -89,9 +102,6 @@ content.add(Chunk.NEWLINE); content.add(Chunk.NEWLINE); - // add credits - content.add(new Paragraph("MPIWG Berlin 2009", FontFactory.getFont(FontFactory.HELVETICA,10))); - // add digilib version content.add(new Paragraph(getDigilibVersion(),FontFactory.getFont(FontFactory.HELVETICA,10))); @@ -109,32 +119,11 @@ return content; } - /** - * return base directory of an image directory - * - * @param path - * @return - */ - private String getBase(String path){ - if(path.contains("/")){ - String[] x = path.split("/"); - String newpath = ""; - for(int i=0; i<x.length-1; i++){ - newpath += x[i]+"/"; - } - return newpath; - } - else - return ""; - } - - - /** + /* * Methods for the different attributes. * */ - private Image getLogo(){ try { URL url = new URL(job_info.getDlConfig().getAsString("pdf-logo")); @@ -145,34 +134,35 @@ } } catch (BadElementException e) { logger.error(e.getMessage()); - e.printStackTrace(); } catch (MalformedURLException e) { logger.error(e.getMessage()); - e.printStackTrace(); } catch (IOException e) { logger.error(e.getMessage()); - e.printStackTrace(); } return null; } + private String getTitle(){ if(info_reader.hasInfo()) return info_reader.getAsString("title"); else return job_info.getImageJobInformation().getAsString("fn"); } + private String getAuthor(){ if(info_reader.hasInfo()) return info_reader.getAsString("author"); else return " "; } + private String getDate(){ if(info_reader.hasInfo()) return info_reader.getAsString("date"); else return " "; } + private String getPages(){ return "Pages "+job_info.getAsString("pgs") + " (scan page numbers)"; }
--- a/servlet/src/digilib/servlet/PDFCache.java Thu Jan 06 20:51:58 2011 +0100 +++ b/servlet/src/digilib/servlet/PDFCache.java Fri Jan 07 17:47:24 2011 +0100 @@ -1,8 +1,6 @@ package digilib.servlet; -import java.io.BufferedInputStream; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.concurrent.Future; @@ -11,7 +9,6 @@ import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; -import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -23,7 +20,7 @@ import digilib.util.DigilibJobCenter; /** - * A class for handling user requests for pdf documents from digilib images. + * A class for handling user requests for pdf documents made from digilib images. * * If a document does not already exist, it will be enqueued for generation; if it does exist, it is sent * to the user. @@ -174,74 +171,82 @@ try { createNewPdfDocument(pdfji, docid); notifyUser(status, docid, request, response); + return; } catch (FileNotFoundException e) { // error in pdf creation logger.error(e.getMessage()); notifyUser(PDFStatus.ERROR, docid, request, response); + return; } } else if (status == PDFStatus.DONE) { // pdf created -- send it try { ServletOps.sendFile(getCacheFile(docid), "application/pdf", getDownloadFilename(pdfji), response, logger); - //sendFile(docid, getDownloadFilename(pdfji), response); + return; } catch (Exception e) { // sending didn't work logger.error(e.getMessage()); + return; } } else { // should be work in progress notifyUser(status, docid, request, response); + return; } } catch (Exception e) { // error in pdf creation logger.error(e.getMessage()); notifyUser(PDFStatus.ERROR, docid, request, response); + return; } } - /** - * depending on the documents status, redirect the user to the appropriate waiting or download page. - * - * @param status - * @param documentid - * @param request - * @param response - */ - public void notifyUser(PDFStatus status, String documentid, HttpServletRequest request, HttpServletResponse response){ - - String jsp=null; - - if(status == PDFStatus.NONEXISTENT){ - // tell the user that the document has to be created before he/she can download it - logger.debug("PDFCache: "+documentid+" has STATUS_NONEXISTENT."); - jsp = JSP_WIP; - } else if(status == PDFStatus.WIP){ - logger.debug("PDFCache: "+documentid+" has STATUS_WIP."); - jsp = JSP_WIP; + /** + * depending on the documents status, redirect the user to the appropriate + * waiting or download page. + * + * @param status + * @param documentid + * @param request + * @param response + */ + public void notifyUser(PDFStatus status, String documentid, + HttpServletRequest request, HttpServletResponse response) { + + String jsp = null; + + if (status == PDFStatus.NONEXISTENT) { + // tell the user that the document has to be created before he/she + // can download it + logger.debug("PDFCache: " + documentid + " has STATUS_NONEXISTENT."); + jsp = JSP_WIP; + } else if (status == PDFStatus.WIP) { + logger.debug("PDFCache: " + documentid + " has STATUS_WIP."); + jsp = JSP_WIP; - // TODO: estimate remaining work time - // TODO: tell the user he/she has to wait - } else if(status == PDFStatus.DONE){ - logger.debug("PDFCache: "+documentid+" has STATUS_DONE."); - } else { - logger.debug("PDFCache: "+documentid+" has STATUS_ERROR."); - jsp = JSP_ERROR; - } + // TODO: estimate remaining work time + // TODO: tell the user he/she has to wait + } else if (status == PDFStatus.DONE) { + logger.debug("PDFCache: " + documentid + " has STATUS_DONE."); + } else { + logger.debug("PDFCache: " + documentid + " has STATUS_ERROR."); + jsp = JSP_ERROR; + } - try { - // forward to the relevant jsp - ServletContext context = getServletContext(); - RequestDispatcher dispatch = context.getRequestDispatcher(jsp); - dispatch.forward(request, response); - } catch (ServletException e) { - logger.debug(e.getMessage()); - e.printStackTrace(); - } catch (IOException e) { - logger.debug(e.getMessage()); - e.printStackTrace(); - } - - } + try { + // forward to the relevant jsp + ServletContext context = getServletContext(); + RequestDispatcher dispatch = context.getRequestDispatcher(jsp); + dispatch.forward(request, response); + } catch (ServletException e) { + logger.debug(e.getMessage()); + e.printStackTrace(); + } catch (IOException e) { + logger.debug(e.getMessage()); + e.printStackTrace(); + } + + } /** check the status of the document corresponding to the documentid */ @@ -293,56 +298,7 @@ return filename; } - - /** - * sends a document to the user - * - * @param cachefile The filename of the document in cache. - * @param filename The filename used for downloading. - * @param response - * @throws IOException - */ - public void sendFile(String cachefile, String filename, HttpServletResponse response) throws IOException{ - File cached_file = null; - FileInputStream fis = null; - ServletOutputStream sos = null; - BufferedInputStream bis = null; - - try { - // get file handle - cached_file = new File(cache_directory, cachefile); - // create necessary streams - fis = new FileInputStream(cached_file); - sos = response.getOutputStream(); - bis = new BufferedInputStream(fis); - - int bytes = 0; - - // set http headers - response.setContentType("application/pdf"); - response.addHeader("Content-Disposition", "attachment; filename="+filename); - response.setContentLength( (int) cached_file.length()); - - // send the bytes - while ((bytes = bis.read()) != -1){ - sos.write(bytes); - } - } - catch(Exception e){ - logger.error(e.getMessage()); - } - finally{ - // close all streams - if (fis != null) - fis.close(); - if (bis != null) - bis.close(); - if (sos != null) - sos.close(); - } - - } - + public File getCacheDirectory(){ return cache_directory; }
--- a/servlet/src/digilib/servlet/Scaler.java Thu Jan 06 20:51:58 2011 +0100 +++ b/servlet/src/digilib/servlet/Scaler.java Fri Jan 07 17:47:24 2011 +0100 @@ -32,7 +32,7 @@ public class Scaler extends HttpServlet { /** digilib servlet version (for all components) */ - public static final String version = "1.8.2b"; + public static final String version = "1.8.3b"; /** servlet error codes */ public static enum Error {UNKNOWN, AUTH, FILE, IMAGE};