简体   繁体   English

使用 jsPDF 将 html 转换为 pdf 时丢失内容

[英]Losing content when i convert html to pdf with jsPDF

In my React App (client-side) i have a button to create a pdf using the current html content with jsPDF .在我的 React 应用程序(客户端)中,我有一个按钮可以使用当前的 html 内容和jsPDF创建一个 pdf 。 I'm using .html() instead of .fromHTML() because i need to keep the html styles in the pdf conversion.我使用.html()而不是.fromHTML()因为我需要在 pdf 转换中保留 html 样式。 The Real Problem is when i use html() i lost a lot of content for the row/col structure of the page.真正的问题是当我使用html() 时,我丢失了很多页面行/列结构的内容。 So, i need to scale my html content into the pdf, but i don't know how to do it.所以,我需要将我的 html 内容缩放到 pdf 中,但我不知道该怎么做。

My code:我的代码:

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

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

By "lost a lot of content" if you meant your content cannot fit into an a4 page then you can either make your page larger, say using a3 instead of a4, or you may shrink the content by using scale in html2canvas .通过“丢失大量内容”,如果您的意思是您的内容无法放入 a4 页面,那么您可以使页面变大,例如使用 a3 而不是 a4,或者您可以通过在html2canvas使用scale来缩小内容。 The scale can be calculated based on your page format and element size.可以根据您的页面格式和元素大小计算比例。

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

If you don't need a standard page size, you may just set the page according to your content.如果您不需要标准页面大小,您可以根据您的内容设置页面。 eg let pdf = new jsPDF('p', 'pt', [612, 1200]);例如let pdf = new jsPDF('p', 'pt', [612, 1200]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM