[英]setting pdf page width/height when using jsPDF
I'm trying to use the jsPDF lib to generate a pdf. 我正在尝试使用jsPDF lib生成pdf。 I'm able to build the pdf but is there a way to pass in the page size to each page of the pdf?
我能够构建pdf,但有没有办法将页面大小传递给pdf的每个页面? I'm currently getting output to the pdf with some extra white space below each of the 'images' that I add.
我目前正在输出pdf,并在我添加的每个“图像”下方留出一些额外的空白区域。
here is a screen shot of one of the pages on the pdf. 这是pdf上其中一个页面的屏幕截图。 It has the image and then there is white space that fills the rest of that page.
它有图像,然后有白色空间填充该页面的其余部分。 I also have white space on the right because the width is wider than my image as well.
我的右侧也有空白区域,因为宽度也比我的图像宽。 The width isn't as much of an issue as the height problem.
宽度不像高度问题那么大。 I'm sure I can scoot the images over by setting the x, y values on the following line:
我确定我可以通过在下一行设置x,y值来扫描图像:
pdf.addImage(img, 'JPEG', 0, 0);
But that won't help me with the height. 但这对身高无济于事。
I'm currently just pumping images into the pdf 1 per page and I would like all the pages to be the same size. 我目前只是将图像分成每页1页的pdf,我希望所有页面的大小相同。 I will know the size of the images and would like to pass options in like this:
我会知道图像的大小,并希望像这样传递选项:
var pdf = new jsPDF(options, '', '', '');
or 要么
pdf.addPage(options);
Is it possible to set the width and height of the pdf or the individual pages of the pdf? 是否可以设置pdf的宽度和高度或pdf的各个页面?
You should be able to choose a page format that is close enough to what you want when creating the pdf like so: 在创建pdf时,您应该能够选择一个足够接近您想要的页面格式,如下所示:
var pdf = new jsPDF("l", "pt", "letter");
That third parameter comes from the pageFormats object at the top of the jsPDF declaration in the file. 第三个参数来自文件中jsPDF声明顶部的pageFormats对象。
Also, you should be able to scale your images so that they fill the appropriate size on the page. 此外,您应该能够缩放图像,使它们在页面上填充适当的大小。 If you look at the prototype for the addImage function, you will see that you can pass:
如果你看一下addImage函数的原型,你会发现你可以传递:
addImage(imageData, format, x, y, w, h, alias, compression);
where the w and h are the expected bounds of your image. 其中w和h是图像的预期边界。 In my experience, jsPDF has no problem scaling the image down from the source, so I see no reason why it can't scale up.
根据我的经验,jsPDF在从源头缩小图像没有问题,所以我认为没有理由不能扩展它。
EDIT: 编辑:
Note that if you're intent on creating the pdf as var pdf = new jsPDF(options, '', '', '');
请注意,如果您打算将pdf创建为
var pdf = new jsPDF(options, '', '', '');
then you can set options as: 然后您可以将选项设置为:
options = {
orientation: "l",
unit: "pt",
format: "letter"
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.