2013-05-27 104 views
0

我正在使用YaHP-Converter將HTML文件轉換爲PDF。這裏是我用於轉換的代碼示例。代碼工作正常。但是我想在轉換後打開Pdf文件。 請任何想法。如何轉換HTML到PDF並打開PDF文件,使用Java與YaHP Html到Pdf轉換器

CYaHPConverter converter = new CYaHPConverter(); 
FileOutputStream out = new FileOutputStream(pdfOut); 
Map properties = new HashMap(); 
List headerFooterList = new ArrayList(); 
properties.put(IHtmlToPdfTransformer.PDF_RENDERER_CLASS,IHtmlToPdfTransformer.FLYINGSAUCER_PDF_RENDERER); 
converter.convertToPdf(htmlContents, 
      IHtmlToPdfTransformer.LEGALL, 
      headerFooterList, 
      "file:///D:/temp/", 
      out, 
      properties); 

在此先感謝

回答

2

我認爲這有助於:

   import java.io.File; 
       import java.io.FileOutputStream; 
       import java.util.ArrayList; 
       import java.util.HashMap; 
       import java.util.List; 
       import java.util.Map; 
       import java.util.Scanner; 


       // http://www.allcolor.org/YaHPConverter/ 
       import org.allcolor.yahp.converter.CYaHPConverter; 
       import org.allcolor.yahp.converter.IHtmlToPdfTransformer; 

       public class HtmlToPdf_yahp_2 { 
        public static void main(String ... args) throws Exception { 
        String root = "c:/temp/html"; 
        String input = "file_1659686.htm"; // need to be charset utf-8 
        htmlToPdfFile(new File(root, input), 
            new File(root, input + ".pdf")); 
        System.out.println("Done"); 
        } 

        public static void htmlToPdfFile(File htmlIn, File pdfOut) throws Exception { 
        Scanner scanner = 
         new Scanner(htmlIn).useDelimiter("\\Z"); 
        String htmlContents = scanner.next(); 

        CYaHPConverter converter = new CYaHPConverter(); 
        FileOutputStream out = new FileOutputStream(pdfOut); 
        Map properties = new HashMap(); 
        List headerFooterList = new ArrayList(); 

        properties.put(IHtmlToPdfTransformer.PDF_RENDERER_CLASS, 
            IHtmlToPdfTransformer.FLYINGSAUCER_PDF_RENDERER); 
        //properties.put(IHtmlToPdfTransformer.FOP_TTF_FONT_PATH, fontPath); 
        converter.convertToPdf(htmlContents, 
           IHtmlToPdfTransformer.A4P, 
           headerFooterList, 
           "file:///temp/html/", 
           out, 
           properties); 
        out.flush(); 
        out.close(); 
        } 
       } 

獲得進一步的信息請參閱本:

http://www.rgagnon.com/javadetails/java-convert-html-to-pdf-using-yahp.html

+0

感謝您的快速回復。隨着你的參考,我已成功創建PDF文件。但是我需要在創建它之後自動打開該文件。因爲上面的代碼只能創建pdf文件。不能自動打開文件。請任何想法。 –