首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用SVG创建以下HTML字符串的PDF文档?

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

Stack Overflow用户
提问于 2014-06-30 14:02:07
回答 1查看 2.1K关注 0票数 4

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

  1. 将HTML转换为org.w3c.dom.Document
  2. 使用生成pdf

进口java.io.FileNotFoundException;进口java.io.FileOutputStream;进口java.io.IOException;进口java.io.OutputStream;进口java.io.StringReader;进口javax.xml.parsers.DocumentBuilder;进口javax.xml.parsers.DocumentBuilderFactory;进口java.io.StringReader进口org.w3c.dom.Document;进口org.xhtmlrenderer.layout.SharedContext;进口org.xhtmlrenderer.pdf.ITextRenderer;进口org.xml.sax.InputSource;进口org.xml.sax.SAXException;进口com.lowagie.text.DocumentException;

代码语言:javascript
运行
复制
    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

EN

回答 1

Stack Overflow用户

发布于 2015-04-10 10:56:48

您可以尝试使用第三方工具将您的html文件转换为pdf格式。它主要处理所有的事情。

wkhtmltopdf是一种很好的查看方式。

How to use and improve wakhtmltopdf performance?

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24491917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档