我在一个将html转换成pdf的情况下,谢天谢地我可以通过飞碟api实现这一点。但是我的HTML由svg标记组成,而转换时我无法在pdf中获得svg。它可以使用堆栈过流问题和教程来实现。
replacedElementFactory
是什么意思?
ChainingReplacedElementFactory chainingReplacedElementFactory
= new ChainingReplacedElementFactory();
chainingReplacedElementFactory.addReplacedElementFactory(replacedElementFactory);
chainingReplacedElementFactory.addReplacedElementFactory(new SVGReplacedElementFactory());
renderer.getSharedContext().setReplacedElementFactory(chainingReplacedElementFactory);
发布于 2016-05-06 08:23:18
这只是教程中的一个错误,不需要使用replacedElementFactory
的行。
这是我的工作例子。
爪哇:
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:
<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>
ChainingReplacedElementFactory
,SVGReplacedElement
和SVGReplacedElementFactory
来自教程。
发布于 2016-05-06 18:25:04
如果您想要一个页面中的解决方案,这里有一个使用@cloudformatter的替代方案,它是一个远程格式化服务。我添加了他们的Javascript到你的小提琴,连同一些文字和你的高图表。
http://jsfiddle.net/yk0Lxzg0/1/
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格式。
发布于 2017-03-24 16:29:59
@Rajesh我希望你已经找到了解决你的问题的方法。如果没有(或者任何人有问题处理飞碟、蜡染和svg标签),那么您可能想考虑这一点--从clip-path="url(#highcharts-xxxxxxx-xx)"
标记中删除所有的<g>
标记对我来说很有用。
https://stackoverflow.com/questions/37056791
复制相似问题