简体   繁体   English

使用jsPDF时设置pdf页面宽度/高度

[英]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. 但这对身高无济于事。

屏幕截图的PDF格式

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.

相关问题 JSPDF-如何将具有不同页面大小(高度和宽度)的多个图像导出到单个pdf文件 - JSPDF - How to export multiple images with various page size (height & width) to a single pdf file jsPDF 如何将图像设置为适合各种页面大小的高度和宽度到单个 PDF 文件 - jsPDF How to set images to fit with various page size height & width to a single PDF file jsPDF / AutoTable生成的PDF重叠页面宽度 - jsPDF/AutoTable Generated PDF overlap page width 如何在使用 JSPDF 新 html API 从 html 生成 pdf 时给出宽度、高度、x 和 y 坐标 - How to give width, height, x and y coordinates in generating pdf from html using JSPDF new html API 使用 javascript 在添加的 jspdf 上为每个新页面声明新的高度和宽度 - Declare new height and width for every new page on added jspdf using javascript 使用jsPDf将网页导出为PDF - Exporting web page into PDF using jsPDf 未使用 jsPDF 呈现为 pdf 的页面图像 - Images of page not rendered to pdf using jsPDF 使用jsPDF和jsPDF autotable创建pdf时为空行 - Empty rows when creating pdf using jsPDF and jsPDF autotable 使用jspdf将HTML的表格转换为pdf时,分页符无法正确绘制表格边框 - When tables from HTML are converted to pdf using jspdf,table borders are not drawn properly when the page breaks 如何使用jsPDF设置生成pdf的列宽 - How to set column width for generating pdf using jsPDF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM