我想将html canvas导出为pdf并下载,我进行了一些搜索,以下脚本对我来说效果很好:
<script language = "JAVASCRIPT">
var canvas = document.getElementById('mycanvas');
var context = canvas.getContext('2d');
// draw a blue cloud
context.beginPath();
context.moveTo(170, 80);
context.bezierCurveTo(130, 100, 130, 150, 230, 150);
context.bezierCurveTo(250, 180, 320, 180, 340, 150);
context.bezierCurveTo(420, 150, 420, 120, 390, 100);
context.bezierCurveTo(430, 40, 370, 30, 340, 50);
context.bezierCurveTo(320, 5, 250, 20, 250, 50);
context.bezierCurveTo(200, 5, 150, 20, 170, 80);
context.closePath();
context.lineWidth = 5;
context.fillStyle = '#8ED6FF';
context.fill();
context.strokeStyle = '#0000ff';
context.stroke();
download.addEventListener("click", function() {
// only jpeg is supported by jsPDF
var imgData = canvas.toDataURL("image/jpeg", 1.0);
var pdf = new jsPDF();
pdf.addImage(imgData, 'JPEG', 0, 0);
var download = document.getElementById('download');
pdf.save("download.pdf");
}, false);
</script>
但是我的画布有黑色背景!
我尝试了其他代码和许多在Internet上找到的解决方案,尝试更改canvas标签中的背景颜色,但没有进行..
有什么帮助吗?
最佳答案
代替以JPEG
的方式下载,请尝试以PNG
的方式下载:
download.addEventListener("click", function() {
// only jpeg is supported by jsPDF
var imgData = canvas.toDataURL("image/png", 1.0);
var pdf = new jsPDF();
pdf.addImage(imgData, 'PNG', 0, 0);
var download = document.getElementById('download');
pdf.save("download.pdf");
}, false);
关于javascript - 将canvas转换为pdf:黑色背景,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37244994/