繁体   English   中英

如何使用SVG创建包含以下HTML字符串的PDF文档?

[英]How to create a PDF document with the following HTML string with SVG?

我正在尝试将HTML字符串转换为PDF文档。 但只有文本部分被打印到PDF而不是SVG。 这是我尝试过的。

  1. 将HTML字符串转换为org.w3c.dom.Document
  2. 使用Flying-Saucer生成pdf
 import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.xhtmlrenderer.layout.SharedContext; import org.xhtmlrenderer.pdf.ITextRenderer; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import com.lowagie.text.DocumentException; 
    public class PDFCreator {
    public static void main(String[] args) {
        PDFCreator pdfCreator = new PDFCreator();
        String html = "<!DOCTYPE html> <html> <head> <meta name=\"generator\" content=\"HTML Tidy for Java (vers. 2009-12-01), see jtidy.sourceforge.net\" /> <style type=\"text/css\"> #svg {display:block;}</style> <title>Sample Document</title> </head> <body> <h1>My First HTML Document with SVG</h1> <div id=\"svg\"> <svg width=\"100\" height=\"100\"> <circle cx=\"50\" cy=\"50\" r=\"40\" stroke=\"green\" stroke-width=\"4\" fill=\"yellow\" /></svg></div> </body></html>";
        Document document = pdfCreator.createDocument(html);
        pdfCreator.create(document);
    }

    public void create(Document document) {
        try {
            String outputFile = "D:\\htmlWithSVG.pdf";
            OutputStream os = new FileOutputStream(outputFile);
            ITextRenderer renderer = new ITextRenderer();
            renderer.setDocument(document, null);
            ChainingReplacedElementFactory chainingReplacedElementFactory = new ChainingReplacedElementFactory();
            chainingReplacedElementFactory.addReplacedElementFactory(new SVGReplacedElementFactory());
            SharedContext sharedContext = renderer.getSharedContext();
            sharedContext.setReplacedElementFactory(chainingReplacedElementFactory);
            renderer.layout();
            renderer.createPDF(os);
            os.close();
        } catch (DocumentException ex) {
            ex.printStackTrace();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public Document createDocument(String xml) {
        InputSource inputSource = new InputSource(new StringReader(xml));
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = null;
        Document document = null;
        try {
            builder = factory.newDocumentBuilder();
            document = builder.parse(inputSource);
        } catch (ParserConfigurationException ex) {
            ex.printStackTrace();
        } catch (SAXException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return document;
    }}

我还查看了以下教程,并从该教程中使用了ChainingReplacedElementFactorySVGReplacedElementFactory类。

http://www.samuelrossille.com/home/render-html-with-svg-to-pdf-with-flying-saucer.html

您可以尝试使用第三方工具将您的html文件转换为PDF格式。 它主要处理一切。

wkhtmltopdf是一个很好看的方式。

如何使用和改善wakhtmltopdf性能?

这是我发布的链接。 我试过c#

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM