javascript - 当我使用 jsPDF 将 html 转换为 pdf 时丢失内容

标签 javascript html reactjs pdf jspdf

在我的 React 应用程序(客户端)中,我有一个按钮可以使用当前的 html 内容与 jsPDF 创建 pdf。我使用 .html() 而不是 .fromHTML() 因为我需要在 pdf 转换中保留 html 样式。 真正的问题是当我使用html()时,我丢失了页面的行/列结构的大量内容。所以,我需要将我的 html 内容缩放到 pdf 中,但我不知道该怎么做。

我的代码:

let doc = new jsPDF('p', 'pt', 'a4')
    let source = document.getElementById('pdfContent')

    doc.html( source, {
        callback: function (doc) {
            doc.save();
        }
     });

最佳答案

“丢失大量内容”如果您的意思是您的内容无法放入 a4 页面,那么您可以使页面更大,例如使用 a3 而不是 a4,或者您可以使用 scale 缩小内容html2canvas 中。可以根据您的页面格式和元素大小计算比例。

let doc = new jsPDF('p', 'pt', 'a4')

doc.html(document.getElementById('pdfContent'), {
    html2canvas: {
        scale: [yourScaleHere],
    },
    x: [margin],
    y: [margin],
    callback: function (doc) {
        window.open(doc.output('bloburl'));
    }
});

如果您不需要标准页面大小,您可以根据您的内容设置页面。例如let pdf = new jsPDF('p', 'pt', [612, 1200]);

关于javascript - 当我使用 jsPDF 将 html 转换为 pdf 时丢失内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60233354/

相关文章:

javascript - 最后有暂停的文本旋转器

php - 在 HTMl 和 PHP 中向表单添加字段

reactjs - simpleWebRTC RemoteMedia 视频不包含正确的 screenCapture bool

node.js - 如何在 React Native WebView 中自定义网站 CSS?

javascript - 从 url 中删除查询字符串 - HashRouter

javascript - Angular : modifying parent controller from children

javascript - 如何在 React 中使用 # 个 url 来保存组件应用程序状态?

javascript - 使用javascript设置IFrame allowfullscreen

html - 使用自定义组件在 Vue 中渲染 TR

html - 如何在不使用最小宽度或精确固定宽度的情况下使元素水平滚动?