【问题标题】:Image in PDF cut off: How to make a canvas fit entirely in a PDF page?PDF 中的图像被截断:如何使画布完全适合 PDF 页面?
【发布时间】:2015-06-17 04:38:05
【问题描述】:

当使用 jspdf 库将画布放置在 PDF 中时,图像会被截断。

html2canvas(myContainer, {background: 'red'}).then (canvas) ->
  imgData = canvas.toDataURL('image/jpeg', 1.0)
  window.open(imgData) # this is just a test that opens the image in a new tab and it displays nicely, the entire image
  pdf = new jsPDF("l", "pt", "b1") # tried a variety of formats but no luck
  pdf.addImage(imgData, 'JPEG', 0, 0)
  pdf.save('file.pdf') # the generated pdf that contains the image gets trimmed

有没有人知道如何使画布适合?

【问题讨论】:

  • 控制台有错误吗?
  • @KenFyrstenberg 没有错误。从 HTML 制作画布并将其放入 PDF 中一切都很好,只是它不完全适合并最终将画布/图像的其余部分剪掉,就像它不在乎一样。

标签: canvas html2canvas jspdf


【解决方案1】:

也尝试设置图像的宽度和高度(无论如何,addImage 的文档似乎很少):

var pdf = new jsPDF("l", "mm", "a4");
var imgData = canvas.toDataURL('image/jpeg', 1.0);

// due to lack of documentation; try setting w/h based on unit
pdf.addImage(imgData, 'JPEG', 10, 10, 180, 150);  // 180x150 mm @ (10,10)mm

【讨论】:

  • 你刚刚拯救了我的周末!
  • 我已经尝试过,我正在使用纵向视图,设置 var pdf = new jsPDF("p", "mm", "a4");pdf.addImage(imgData, 'JPEG', 10, 10, 150, 180); 但这不起作用:(有什么建议吗?
【解决方案2】:

我有一个解决方案:

function saveImageToPdf(idOfHtmlElement)
{
   var fbcanvas = document.getElementById(idOfHtmlElement);
   html2canvas($(fbcanvas),
        {

            onrendered: function (canvas) {

                var width = canvas.width;
                var height = canvas.height;
                var millimeters = {};
                millimeters.width = Math.floor(width * 0.264583);
                millimeters.height = Math.floor(height * 0.264583);

                var imgData = canvas.toDataURL(
                    'image/png');
                var doc = new jsPDF("p", "mm", "a4");
                doc.deletePage(1);
                doc.addPage(millimeters.width, millimeters.height);
                doc.addImage(imgData, 'PNG', 0, 0);
                doc.save('WebSiteScreen.pdf');
            }
        });
}

你要做的事情是:

  1. 将像素转换为毫米。
  2. 先删除未正确调整大小的页面
  3. 创建一个以毫米为单位的尺寸合适的新文件。
  4. 保存图片。

【讨论】:

  • 很好的解决方案。您是如何获得转换编号 0.264583 的?我目前的理解是需要pdf的dpi值来确定转换数。
【解决方案3】:

看起来图片的大小和pdf的页面大小不一样。您可以设置页面大小,即。添加具有与图像相似的定义高度和宽度的页面,或者您可以在将图像添加到 pdf 时为图像设置选项。

方法 1 的示例:Answered above
方法二的例子:

    var pdf = new jsPDF("l", "mm", "a4");   //orientation: landscape
    var imgData = canvas.toDataURL('image/png');
    var width = pdf.internal.pageSize.getWidth();
    var height = pdf.internal.pageSize.getHeight();
    pdf.addImage(imgData, 'PNG', 0, 0, width, height); 
    pdf.save('download.pdf');

【讨论】:

    【解决方案4】:

    对于我的用例,这是对我有用的解决方案:

    var canvas = document.getElementById('canvas');
    var imgData = canvas.toDataURL("image/png");
    var pdf = new jsPDF('p', 'pt', [canvas.width, canvas.height]);
    var pdfWidth = pdf.internal.pageSize.getWidth();
    var pdfHeight = pdf.internal.pageSize.getHeight();
    pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight);
    pdf.save("mypdf.pdf");
    

    【讨论】:

      【解决方案5】:

      我指定了类似的内容。看起来gd。请尝试:

      html2canvas(input)  
                .then((canvas) => {  
                  var imgWidth = 350;  
                  var imgHeight = canvas.height * imgWidth / canvas.width;  
                  const imgData = canvas.toDataURL('image/png', 1.0);  
                  const pdf = new jsPDF('l', 'mm', 'a4')  
                  
                  pdf.addImage(imgData, 'JPEG', -25 , 5,imgWidth , imgHeight);  
      
                  pdf.save("download.pdf");  
                });  
      

      【讨论】:

        【解决方案6】:

        我的全宽自动高度解决方案可在需要时扩展页面高度

          const document = new jsPDF();
          const imgProps= document.getImageProperties(imageToAdd);
          const width = document.internal.pageSize.getWidth();
          const ratio = width/ imgProps.width;
          const height = ratio * imgProps.height;
          document.internal.pageSize.height = height;
          document.addImage(this.doc, 'PNG', 0, 0, width, height);
        

        【讨论】:

          猜你喜欢
          • 2021-11-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-06
          相关资源
          最近更新 更多