PDF 中的图像截断:如何使 canvas 完全适合 PDF 页面?

Image in PDF cut off: How to make a canvas fit entirely in a PDF page?

使用 jspdf 库在 PDF 中放置 canvas 时,图像会被截断。

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

有没有人知道如何使 canvas 合身?

也尝试设置图像的宽度和高度(无论如何,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

我有一个解决方案给你:

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. 保存图像。

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

方法 1 的示例:
方法 2 的示例:

    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');

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

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");

我指定了这样的东西。看起来不错。请尝试:

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");  
          });  

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

  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);