0

我正在使用 YaHP 转换器将 HTML 文件转换为 Pdf。这是我用于转换的代码示例。该代码对我来说很好。但我想在此转换后打开 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);

提前致谢

4

1 回答 1

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

于 2013-05-27T05:22:04.950 回答