最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Resize html before exporting to pdf with jspdf .html() method - Stack Overflow

programmeradmin2浏览0评论

I am trying to figure out how to resize a HTML element with jsPDF .html() method.

I tried with the code below but the width option does not seem to make any change in the output.

I am just assuming width is an option of .html() as unfortunately, as of now, there's no documentation to this method and one is supposed to guess it from the code.

Also, it seems to me it should takes the option from the html2canvas options, that's why I tried with width.

Anyway, the code:

var legend = document.getElementById("my-table");
var pdf = new jsPDF(orientation, undefined, format);
pdf.html(legend, {
    width: 200,
    callback: function () {
        window.open(pdf.output('bloburl'));
     }
});

#my-table

<table>
    <tr>
        <td><img src="image1.jpeg"></td>
    </tr>
    <tr>
        <td><img src="image2.jpeg"></td>
    </tr>
    ...
</table>

I am trying to figure out how to resize a HTML element with jsPDF .html() method.

I tried with the code below but the width option does not seem to make any change in the output.

I am just assuming width is an option of .html() as unfortunately, as of now, there's no documentation to this method and one is supposed to guess it from the code.

Also, it seems to me it should takes the option from the html2canvas options, that's why I tried with width.

Anyway, the code:

var legend = document.getElementById("my-table");
var pdf = new jsPDF(orientation, undefined, format);
pdf.html(legend, {
    width: 200,
    callback: function () {
        window.open(pdf.output('bloburl'));
     }
});

#my-table

<table>
    <tr>
        <td><img src="image1.jpeg"></td>
    </tr>
    <tr>
        <td><img src="image2.jpeg"></td>
    </tr>
    ...
</table>
Share Improve this question edited Feb 21, 2019 at 10:38 umbe1987 asked Feb 21, 2019 at 10:03 umbe1987umbe1987 3,5887 gold badges43 silver badges73 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 7

Passing options to html2canvas:

pdf.html(legend, {
    html2canvas: {
        // insert html2canvas options here, e.g.
        width: 200
    },
    callback: function () {
        // ...
    }
});

For me, the option scale works fine for resizing. You can retrieve the default page dimensions listed in the source to calculate the required scaling factor.

I didn't find a way to resize HTML objects. Instead, you can change the measurement unit from mm (default) to pt. This will reduce all sizes to about 1/3 from the original. For example:

doc = new jsPDF('portrait', 'pt', 'a4');

IMPORTANT: any other objects (i.e. text, lines, etc) will reduce too, in size and position. So be careful.

I don't know exactly about jspdf is, but it seems you've miss spelled on

var legend = document.getElementsById("my-table");

here.

var legend = document.getElementById("my-table");

and maybe, you could resize those tables via css or style attribute.

<table style="width:200;">
    <tr>
        <td><img src="image1.jpeg"></td>
    </tr>
    <tr>
        <td><img src="image2.jpeg"></td>
    </tr>
    ...
</table>

using css

#my-table {
 width:200;
}
发布评论

评论列表(0)

  1. 暂无评论