首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >利用飞碟在pdf中的Svg集成

利用飞碟在pdf中的Svg集成
EN

Stack Overflow用户
提问于 2016-05-05 17:33:35
回答 4查看 5.5K关注 0票数 7

我在一个将html转换成pdf的情况下,谢天谢地我可以通过飞碟api实现这一点。但是我的HTML由svg标记组成,而转换时我无法在pdf中获得svg。它可以使用堆栈过流问题教程来实现。

replacedElementFactory是什么意思?

代码语言:javascript
运行
复制
ChainingReplacedElementFactory chainingReplacedElementFactory 
        = new ChainingReplacedElementFactory();
chainingReplacedElementFactory.addReplacedElementFactory(replacedElementFactory);
chainingReplacedElementFactory.addReplacedElementFactory(new SVGReplacedElementFactory());
renderer.getSharedContext().setReplacedElementFactory(chainingReplacedElementFactory);
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-05-06 08:23:18

这只是教程中的一个错误,不需要使用replacedElementFactory的行。

这是我的工作例子。

爪哇:

代码语言:javascript
运行
复制
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.xhtmlrenderer.pdf.ITextRenderer;

public class PdfSvg {
    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document inputDoc =  builder.parse("svg.html");

        ByteArrayOutputStream output = new ByteArrayOutputStream();

        ITextRenderer renderer = new ITextRenderer();

        ChainingReplacedElementFactory chainingReplacedElementFactory = new ChainingReplacedElementFactory();
        chainingReplacedElementFactory.addReplacedElementFactory(new SVGReplacedElementFactory());
        renderer.getSharedContext().setReplacedElementFactory(chainingReplacedElementFactory);

        renderer.setDocument(inputDoc, "");;
        renderer.layout();
        renderer.createPDF(output);

        OutputStream fos = new FileOutputStream("svg.pdf");
        output.writeTo(fos);
    }
}

HTML:

代码语言:javascript
运行
复制
<html>
<head>
<style type="text/css">
    svg {display: block;width:100mm;height:100mm}
</style>
</head>
<body>
    <div>
        <svg xmlns="http://www.w3.org/2000/svg">
            <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3"
                fill="red" />
        </svg>
    </div>
</body>
</html>

ChainingReplacedElementFactorySVGReplacedElementSVGReplacedElementFactory来自教程

票数 7
EN

Stack Overflow用户

发布于 2016-05-06 18:25:04

如果您想要一个页面中的解决方案,这里有一个使用@cloudformatter的替代方案,它是一个远程格式化服务。我添加了他们的Javascript到你的小提琴,连同一些文字和你的高图表。

http://jsfiddle.net/yk0Lxzg0/1/

代码语言:javascript
运行
复制
var click="return xepOnline.Formatter.Format('printme', {render:'download'})";
jQuery('#buttons').append('<button onclick="'+ click +'">PDF</button>');

上面放置在小提琴中的代码将用“id”打印文件格式化div以供下载。包括你的图表和一些文字。

http://www.cloudformatter.com/CSS2Pdf.APIDoc.Usage显示了使用说明,并且在SVG中有更多的图表示例,这些图表由它们自己或作为页面的一部分与文本、表等一起格式化为PDF格式。

票数 1
EN

Stack Overflow用户

发布于 2017-03-24 16:29:59

@Rajesh我希望你已经找到了解决你的问题的方法。如果没有(或者任何人有问题处理飞碟、蜡染和svg标签),那么您可能想考虑这一点--从clip-path="url(#highcharts-xxxxxxx-xx)"标记中删除所有的<g>标记对我来说很有用。

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

https://stackoverflow.com/questions/37056791

复制
相关文章

相似问题

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