如何使用html画布将SVG图形导出为pdf
How to Export SVG graph to pdf using html canvas
我正在使用D3.js,它在SVG中呈现条形图,但我无法像在HTML5画布中呈现的其他图形一样导出完整的图形初始化为PDF。我怎么做呢?
(function(){
var
form = $('.form'),
cache_width = form.width(),
a4 =[ 1100.28, 580.89]; // for a4 size paper width and height
$('#create_pdf').on('click',function(){
$('body').scrollTop(0);
createPDF();
});
//create pdf
function createPDF(){
getCanvas().then(function(canvas){
var imgData = canvas.toDataURL('image/png');
/*
Here are the numbers (paper width and height) that I found to work.
It still creates a little overlap part between the pages, but good enough for me.
if you can find an official number from jsPDF, use them.
*/
var imgWidth = 210;
var pageHeight = 295;
var imgHeight = canvas.height * imgWidth / canvas.width;
var heightLeft = imgHeight;
var doc = new jsPDF('p', 'mm');
var position = 0;
doc.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
heightLeft -= pageHeight;
while (heightLeft >= 0) {
position = heightLeft - imgHeight;
doc.addPage();
doc.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
heightLeft -= pageHeight;
}
doc.save( 'datamagnified_AE.pdf');
});
}
// create canvas object
function getCanvas(){
form.width((a4[0]*1.33333) -80).css('max-width','none');
return html2canvas(form,{
imageTimeout:2000,
removeContainer:true
});
}
}());
我得到了这个插件工作。在我的html标签的小问题。
** HTML **
<li>
<a title="Download" id="create_pdf" ></a>
</li>
* * jQuery * *
(function(){
var
form = $('.form'),
cache_width = form.width(),
a4 =[ 1100.28, 580.89]; // for a4 size paper width and height
$('#create_pdf').on('click',function(){
$('body').scrollTop(0);
createPDF();
});
//create pdf
function createPDF(){
getCanvas().then(function(canvas){
var imgData = canvas.toDataURL('image/png');
/*
Here are the numbers (paper width and height) that I found to work.
It still creates a little overlap part between the pages, but good enough for me.
if you can find an official number from jsPDF, use them.
*/
var imgWidth = 210;
var pageHeight = 295;
var imgHeight = canvas.height * imgWidth / canvas.width;
var heightLeft = imgHeight;
var doc = new jsPDF('p', 'mm');
var position = 0;
doc.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
heightLeft -= pageHeight;
while (heightLeft >= 0) {
position = heightLeft - imgHeight;
doc.addPage();
doc.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
heightLeft -= pageHeight;
}
doc.save( 'datamagnified_AE.pdf');
});
}
// create canvas object
function getCanvas(){
form.width((a4[0]*1.33333) -80).css('max-width','none');
return html2canvas(form,{
imageTimeout:2000,
removeContainer:true
});
}
}());
尝试画布转换SVG到画布。然后使用. todataurl()将画布转换为base64字符串。JsPdf回购。Canvg回购:.
相关文章:
- 如何使用javascript或html下载PDF格式的填写表单
- 如何将PDF作为二进制文件传递到window.open()
- 有没有一个javascript图形绘制库可以进行气球树布局
- javascript.点击显示嵌入的pdf
- Javascript/Jquery Blob not showing Chrome PDF
- 在提交时打开thankyou.html+下载PDF
- 生成pdf或其他非html文件时的错误处理
- how to convert html <div> to pdf
- 如何在d3上的图形中添加放大和缩小按钮
- 如何在React Native中绘制图形
- 将画布转换为pdf:黑色背景
- 如何在不在本地下载的情况下将url中提供的文件(pdf/doc)转换为json/string/base64格式
- 以PDF附件的形式通过电子邮件发送谷歌文档
- 将Meteor中的base64 PDF作为电子邮件附件
- 无法执行文件.退出Acrobat PDF中的操作
- 如何在Java Web应用程序中的PDF报告中绘制图形
- 如何将包含SVG图形和其他Html元素的Html页面转换为PDF/Image
- 莫里斯图形导出为PDF
- 如何导出多个html表格和多个d3生成的图形成一个单一的pdf
- 如何使用html画布将SVG图形导出为pdf