Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将画布转换为PDF_Javascript_Canvas_Pdf Generation - Fatal编程技术网

Javascript 将画布转换为PDF

Javascript 将画布转换为PDF,javascript,canvas,pdf-generation,Javascript,Canvas,Pdf Generation,是否可以使用JavaScript(或类似的东西)直接将画布转换为pdf 有没有其他可能的方式,比如从画布到img,再从img到pdf 你能给我举个例子吗?你可以通过使用库和函数来实现这一点 我做了一个小小的示范: var canvas=document.getElementById('myCanvas'); var context=canvas.getContext('2d'); //画龙点睛 context.beginPath(); 上下文。moveTo(170,80); 贝塞尔曲线图(13

是否可以使用JavaScript(或类似的东西)直接将画布转换为pdf

有没有其他可能的方式,比如从画布到img,再从img到pdf


你能给我举个例子吗?

你可以通过使用库和函数来实现这一点

我做了一个小小的示范:

var canvas=document.getElementById('myCanvas');
var context=canvas.getContext('2d');
//画龙点睛
context.beginPath();
上下文。moveTo(170,80);
贝塞尔曲线图(130100130150230150);
贝塞尔曲线图(250180320180340150);
贝塞尔曲线图(420150420120390100);
贝塞尔曲线图(430、40370、30340、50);
贝塞尔曲线图(320,5250,20250,50);
贝塞尔曲线图(200,5150,20170,80);
closePath();
context.lineWidth=5;
context.fillStyle='#8ED6FF';
context.fill();
context.strokeStyle='#0000ff';
stroke();
download.addEventListener(“单击”,函数()){
//jsPDF仅支持jpeg
var imgData=canvas.toDataURL(“image/jpeg”,1.0);
var pdf=新的jsPDF();
pdf.addImage(imgData,'JPEG',0,0);
pdf.save(“download.pdf”);
},假)

下载
请参阅。该库创建画布元素的PDF表示,与其他建议的将图像嵌入PDF文档的解决方案不同

//Create a new PDF canvas context.
var ctx = new canvas2pdf.Context(blobStream());

//draw your canvas like you would normally
ctx.fillStyle='yellow';
ctx.fillRect(100,100,100,100);
// more canvas drawing, etc...

//convert your PDF to a Blob and save to file
ctx.stream.on('finish', function () {
    var blob = ctx.stream.toBlob('application/pdf');
    saveAs(blob, 'example.pdf', true);
});
ctx.end();

更好的解决方案是使用剑道ui绘制dom导出为pdf-

假设以下html文件包含canvas标记:

<script src="http://kendo.cdn.telerik.com/2017.2.621/js/kendo.all.min.js"></script>

    <script type="x/kendo-template" id="page-template">
     <div class="page-template">
            <div class="header">

            </div>
            <div class="footer" style="text-align: center">

                <h2> #:pageNum# </h2>
            </div>
      </div>
    </script>
    <canvas id="myCanvas" width="500" height="500"></canvas>
    <button onclick="ExportPdf()">download</button>
就是这样,在画布上写下任何东西,只需按下下载按钮,所有内容都将导出为PDF格式。 以下是剑道UI的链接- 还有一个更好地了解整个过程的博客-

今天,jspdf-1.5.3。 回答pdf文件页面与画布完全相同的问题。在多次尝试不同的组合后,我想你应该做这样的事情。 我们首先需要为输出的pdf文件设置正确方向的高度和宽度,否则侧面可能会被切断。然后我们从“pdf”文件本身获取尺寸,如果您尝试使用画布的尺寸,则侧面可能会再次被切断。我不知道为什么会发生这种情况,我最好的猜测是jsPDF转换库中其他单位的维度

  // Download button
  $("#download-image").on('click', function () {
    let width = __CANVAS.width; 
    let height = __CANVAS.height;

    //set the orientation
    if(width > height){
      pdf = new jsPDF('l', 'px', [width, height]);
    }
    else{
      pdf = new jsPDF('p', 'px', [height, width]);
    }
    //then we get the dimensions from the 'pdf' file itself
    width = pdf.internal.pageSize.getWidth();
    height = pdf.internal.pageSize.getHeight();
    pdf.addImage(__CANVAS, 'PNG', 0, 0,width,height);
    pdf.save("download.pdf");
  });
从这里了解了如何切换方向:

很好,谢谢:)有没有办法导出与img大小(宽度/高度)相同的pdf?这是可能的,但您必须编辑jsPDF库才能做到。嗯。。。已尝试,但addImage行返回错误。。。我想这个函数可能被替换了?@Adil Waqar你把jspdf.js文件作为脚本添加了吗?@MateuszBartkowski,我想这是个错误。它是在有人编辑我的答案以获得代码片段时添加的。由于此库不只是将画布渲染为jpeg并将其放入PDF中,因此它创建了一个高质量/高分辨率pdfgood库,但不支持drawmage(),因此无法在PDF上添加位图图像。已添加图像支持。请参见图片示例,它不支持上下文转换,即ctx.setTransform(2,1,0.5,1,0,1);图像看起来是一样的多层帆布怎么样?很酷,但昂贵。谢谢,你的回答很有帮助。
  // Download button
  $("#download-image").on('click', function () {
    let width = __CANVAS.width; 
    let height = __CANVAS.height;

    //set the orientation
    if(width > height){
      pdf = new jsPDF('l', 'px', [width, height]);
    }
    else{
      pdf = new jsPDF('p', 'px', [height, width]);
    }
    //then we get the dimensions from the 'pdf' file itself
    width = pdf.internal.pageSize.getWidth();
    height = pdf.internal.pageSize.getHeight();
    pdf.addImage(__CANVAS, 'PNG', 0, 0,width,height);
    pdf.save("download.pdf");
  });