canvas - PDF 中的图像被截断 : How to make a canvas fit entirely in a PDF page?

标签 canvas html2canvas jspdf

使用 jspdf 库将 Canvas 放入 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

有人知道如何使 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

关于canvas - PDF 中的图像被截断 : How to make a canvas fit entirely in a PDF page?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41288854/

相关文章:

javascript - 简单的 JavaScript 游戏

javascript - 鼠标位置到等距图 block (包括高度)

javascript - html2pdf 不适用于 Firefox 中的 reactjs

javascript - 更改 html2canvas 以接受显示为无

google-chrome - JsPDF - 不允许将顶部框架导航到数据 URL

angular - 如何在 Angular 7 中使用 jsPDF?获取jspdf__WEBPACK_IMPORTED_MODULE_2__.jsPDF is not a constructor错误

javascript - 截取浏览器页面中容器的屏幕截图

javascript - 这个 Canvas 运动有什么问题

javascript - 如何将 jquery-UI slider 与 canvas rotate() 方法合并?

jspdf - Html2Canvas 未在 iOS 中渲染 Font Awesome 图标