Improved PDF export (every article is a chapter, outline, FileDialog)
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / export / pdf / WikiPDFExporter.java
index 8ab96ef..0b387dd 100644 (file)
@@ -1,6 +1,6 @@
 package net.sourceforge.phpeclipse.wiki.export.pdf;
 
-import java.io.FileNotFoundException;
+import java.awt.Color;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.StringReader;
@@ -25,9 +25,12 @@ import org.eclipse.jface.operation.IRunnableWithProgress;
 import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.dialogs.IOverwriteQuery;
 
+import com.lowagie.text.Chapter;
 import com.lowagie.text.Document;
-import com.lowagie.text.DocumentException;
-import com.lowagie.text.html.HtmlParser;
+import com.lowagie.text.Font;
+import com.lowagie.text.FontFactory;
+import com.lowagie.text.PageSize;
+import com.lowagie.text.Paragraph;
 import com.lowagie.text.pdf.PdfWriter;
 
 //import de.java2html.converter.JavaSource2HTMLConverter;
@@ -182,7 +185,7 @@ public final class WikiPDFExporter implements IRunnableWithProgress {
    * 
    * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
    */
-public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
+  public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
 
     WikiFilesVisitor visitor = new WikiFilesVisitor();
     for (int i = 0; i < fResourcesToExport.size(); i++) {
@@ -196,65 +199,174 @@ public void run(IProgressMonitor monitor) throws InvocationTargetException, Inte
     List list = visitor.getList();
     Collections.sort(list, new IFileComparator());
     IFile file;
-    StringBuffer htmlBuffer = new StringBuffer();
-    htmlBuffer.append("<html><head></head><body>");
-    for (int i = 0; i < list.size(); i++) {
-      try {
-        file = (IFile) list.get(i);
-        monitor.subTask(WikiEditorPlugin.getResourceString("Export.exportFile") + file.getLocation());
-        System.out.println(file.getLocation().toString());
-        // TODO add the real title here:
-        htmlBuffer.append("<h2>"+file.getName()+"</h2><br/>");
-        CreatePageAction.createFragmentPage(file, htmlBuffer);
-        if (i < list.size()-1) {
-//        TODO create a boolean flag to determine, if we would like a new page or only horizontal ruler
-          htmlBuffer.append("<hr/>");
-        }
-        System.out.println(htmlBuffer.toString());
-        //        CreatePageAction.createPage(templateFileName, (IFile) resources[i], exportDirectoryName, srcBasePath);
-        monitor.worked(1);
-      } catch (Exception e) {
-        addError("PDF export exception", e);
-      }
-    }
-    htmlBuffer.append("</body></html>");
-    
-    StringReader stream = null;
-    String pdffilename = fPath.toString();
+
     FileOutputStream os = null;
+    String pdffilename = fPath.toString();
+
+    Document document = new Document(PageSize.A4);
+    PdfWriter pdfWriter = null;
     try {
-      
-      HtmlParser parser = new HtmlParser();
-      Document document = new Document();
-      document.open();
       os = new FileOutputStream(pdffilename);
-      monitor.subTask("Generating PDF file: "+pdffilename);
-      stream = new StringReader(htmlBuffer.toString());
-      PdfWriter pdfWriter = PdfWriter.getInstance(document, os);
-//      pdfWriter.s
-      parser.go(document, stream);
-    } catch (FileNotFoundException e) {
-      e.printStackTrace();
+      pdfWriter = PdfWriter.getInstance(document, os);
+      pdfWriter.setViewerPreferences(PdfWriter.PageModeUseOutlines);
+
+      document.open();
+      document.resetPageCount();
+      int chapterNumber = 1;
+      for (int i = 0; i < list.size(); i++) {
+        try {
+          file = (IFile) list.get(i);
+          if (file.exists()) {
+            monitor.subTask(WikiEditorPlugin.getResourceString("Export.exportFile") + file.getLocation());
+            System.out.println(file.getLocation().toString());
+            StringBuffer htmlBuffer = new StringBuffer();
+            htmlBuffer.append("<html><head></head><body>");
+
+            boolean noContent = CreatePageAction.createFragmentPage(file, htmlBuffer);
+            htmlBuffer.append("</body></html>");
+            System.out.println(htmlBuffer.toString());
+
+            if (!noContent) {
+              // TODO add the real title from corresponding *.xml file here:
+              Paragraph title = new Paragraph(file.getName(), FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC,
+                  new Color(0, 0, 255)));
+              Chapter chapter = new Chapter(title, chapterNumber++);
+              document.add(chapter);
+              appendArticle(document, htmlBuffer);
+            }
+            monitor.worked(1);
+          }
+        } catch (Exception e) {
+          addError("PDF export exception", e);
+        }
+      }
     } catch (Exception e) {
-      e.printStackTrace();
+      addError("PDF export exception", e);
     } finally {
-      if (stream!=null) {
-        stream.close();
+      document.close();
+      if (pdfWriter != null) {
+        pdfWriter.close();
       }
-      if (os!=null) {
+      if (os != null) {
         try {
           os.close();
         } catch (IOException e1) {
         }
       }
+
     }
 
     System.out.println(fPath.toString());
   }
+
+  /**
+   * @param document
+   * @param htmlBuffer
+   * @param monitor
+   */
+  private void appendArticle(Document document, StringBuffer htmlBuffer) {
+    StringReader stream = null;
+    try {
+      WPHtmlParser parser = new WPHtmlParser();
+      stream = new StringReader(htmlBuffer.toString());
+      parser.go(document, stream);
+    } catch (Exception e) {
+      addError("PDF export exception", e);
+    } finally {
+      if (stream != null) {
+        stream.close();
+      }
+    }
+  }
+
+  //  public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
+  //
+  //    WikiFilesVisitor visitor = new WikiFilesVisitor();
+  //    for (int i = 0; i < fResourcesToExport.size(); i++) {
+  //      try {
+  //        ((IResource) fResourcesToExport.get(i)).accept(visitor);
+  //      } catch (CoreException e) {
+  //        e.printStackTrace();
+  //      }
+  //    }
+  //
+  //    List list = visitor.getList();
+  //    Collections.sort(list, new IFileComparator());
+  //    IFile file;
+  //
+  //    FileOutputStream os = null;
+  //    String pdffilename = fPath.toString();
+  //    try {
+  //      os = new FileOutputStream(pdffilename);
+  //      Document document = new Document(PageSize.A4);
+  //      PdfWriter pdfWriter = PdfWriter.getInstance(document, os);
+  //      document.open();
+  //      document.resetPageCount();
+  //      for (int i = 0; i < list.size(); i++) {
+  //        try {
+  //          file = (IFile) list.get(i);
+  //          monitor.subTask(WikiEditorPlugin.getResourceString("Export.exportFile") + file.getLocation());
+  //// System.out.println(file.getLocation().toString());
+  //          StringBuffer htmlBuffer = new StringBuffer();
+  //          // TODO add the real title here:
+  //          htmlBuffer.append("<h2>" + file.getName() + "</h2><br/>");
+  //          boolean noContent = CreatePageAction.createFragmentPage(file, htmlBuffer);
+  //          if (i < list.size() - 1) {
+  //            // TODO create a boolean flag to determine, if we would like a new page or only horizontal ruler
+  //            htmlBuffer.append("<hr/>");
+  //          }
+  //// System.out.println(htmlBuffer.toString());
+  //
+  //          if (!noContent) {
+  //            appendArticle(document, htmlBuffer);
+  //          }
+  //          // CreatePageAction.createPage(templateFileName, (IFile) resources[i], exportDirectoryName, srcBasePath);
+  //          monitor.worked(1);
+  //        } catch (Exception e) {
+  //          addError("PDF export exception", e);
+  //        }
+  //      }
+  //      document.close();
+  //    } catch (Exception e) {
+  //      addError("PDF export exception", e);
+  //    } finally {
+  //      if (os != null) {
+  //        try {
+  //          os.close();
+  //        } catch (IOException e1) {
+  //        }
+  //      }
+  //    }
+  //
+  //    System.out.println(fPath.toString());
+  //  }
+  //
+  //  /**
+  //   * @param document
+  //   * @param htmlBuffer
+  //   * @param monitor
+  //   */
+  //  private void appendArticle(Document document, StringBuffer htmlBuffer) {
+  //    StringReader stream = null;
+  //    try {
+  //// WPHtmlParser parser = new WPHtmlParser();
+  //      stream = new StringReader(htmlBuffer.toString());
+  //// parser.go(document, stream);
+  //      new HTMLWorker(document).parse(stream);
+  //    } catch (Exception e) {
+  //      addError("PDF export exception", e);
+  //    } finally {
+  //      if (stream != null) {
+  //        stream.close();
+  //      }
+  //    }
+  //  }
+
   /**
    * Add a new entry to the error table with the passed information
    */
   protected void addError(String message, Throwable e) {
+    e.printStackTrace();
     errorTable.add(new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, 0, message, e));
   }
 
@@ -282,21 +394,21 @@ public void run(IProgressMonitor monitor) throws InvocationTargetException, Inte
     return new MultiStatus(PlatformUI.PLUGIN_ID, IStatus.OK, errors, "PDF export problems occured", //$NON-NLS-1$
         null);
   }
-  
+
   // test
-//  public static void main(String[] args) {
-//    try {
-//      HtmlParser parser = new HtmlParser();
-//      Document document = new Document();
-//      String htmlfilename = "C:/eclipse/wikibooks/wpbin/Synästhesie.html";
-//      String pdffilename = "C:/eclipse/wikibooks/wpbin/code_java1.pdf";
-//
-//      PdfWriter.getInstance(document, new FileOutputStream(pdffilename));
-//      parser.go(document, htmlfilename);
-//    } catch (FileNotFoundException e) {
-//      e.printStackTrace();
-//    } catch (DocumentException e) {
-//      e.printStackTrace();
-//    }
-//  }
+  //  public static void main(String[] args) {
+  //    try {
+  //      WPHtmlParser parser = new WPHtmlParser();
+  //      Document document = new Document();
+  //      String htmlfilename = "C:/eclipse/wikibooks/wpbin/Synästhesie.html";
+  //      String pdffilename = "C:/eclipse/wikibooks/wpbin/code_java1.pdf";
+  //
+  //      PdfWriter.getInstance(document, new FileOutputStream(pdffilename));
+  //      parser.go(document, htmlfilename);
+  //    } catch (FileNotFoundException e) {
+  //      e.printStackTrace();
+  //    } catch (DocumentException e) {
+  //      e.printStackTrace();
+  //    }
+  //  }
 }
\ No newline at end of file