将画布转换为pdf:黑色背景

时间:2016-05-16 00:07:32

标签: javascript jspdf

我想将我的html画布导出为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>

但是我的画布背景为黑色!

我已经尝试了其他代码和我在互联网上找到的许多解决方案,我试图改变画布标签中的背景颜色,没有用过..

请帮忙吗?

1 个答案:

答案 0 :(得分:0)

请尝试下载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);