Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create custom size PDF #372

Closed
jguix opened this issue Oct 6, 2014 · 10 comments
Closed

Create custom size PDF #372

jguix opened this issue Oct 6, 2014 · 10 comments

Comments

@jguix
Copy link

jguix commented Oct 6, 2014

I suggest the following patch to enable custom PDF size.

The idea is that the format parameter can take an object with width and height properties.

Example:

            var pdf = new jsPDF({
                orientation: widthMm > heightMm ? "L" : "P",
                unit: "mm",
                format: {
                    width: widthMm,
                    height: heightMm
                }
            });

In that example, the format would be widthMm x heightMm. The units of the format are the PDF units. Then, my patch creates a new pageFormat with key "custom" and size translated to standard pt units.

Please find the patch below, it only affects a few lines of the original code:

162a163,181
>       // JGUIX
>       switch (unit) {
>           case 'pt':  k = 1;          break;
>           case 'mm':  k = 72 / 25.4;  break;
>           case 'cm':  k = 72 / 2.54;  break;
>           case 'in':  k = 72;         break;
>           case 'px':  k = 96 / 72;    break;
>           case 'pc':  k = 12;         break;
>           case 'em':  k = 12;         break;
>           case 'ex':  k = 6;          break;
>           default:
>               throw ('Invalid unit: ' + unit);
>       }
>       if (typeof format === 'object') {
>           // Add pageformat in pt units
>           pageFormats['custom'] = [format.width * k, format.height * k];
>           format = 'custom';
>       }
>       // JGUIX
201d219
< 
869,870c887,888
< 
<       switch (unit) {

---
>       // JGUIX
>       /*switch (unit) {
881c899,900
<       }

---
>       }*/
>       // JGUIX
@bolste
Copy link
Contributor

bolste commented Jan 22, 2015

+1

3 similar comments
@michaelkrog
Copy link

+1

@killersdeat0
Copy link

+1

@xande
Copy link

xande commented Sep 11, 2015

+1

@Flamenco
Copy link
Contributor

Try this:

var pdf = new jsPDF('p', 'in', [5,2]);

This will create a 5in x 2in PDF.

@xande
Copy link

xande commented Sep 11, 2015

@Flamenco Thank you much! Couldn't find that in docs.

@josencv
Copy link

josencv commented Sep 11, 2015

You can even set different sizes for every page you add:

var doc = new jsPDF('p', 'mm', [68,20]);
doc.addPage(100, 200);

MrRio added a commit that referenced this issue Jan 30, 2018
Fix for #372, Add Documentation Information
@Uzlopak
Copy link
Collaborator

Uzlopak commented Feb 15, 2018

Solved by PR #1590
Merged on 11 January 2018
Will be part of next version.

@Uzlopak Uzlopak closed this as completed Feb 15, 2018
@jguix
Copy link
Author

jguix commented Feb 15, 2018

Thank you guys!

@entspannt
Copy link

entspannt commented Jun 30, 2018

That would be so nice... Any update, when the next version will be available?

EDIT: Nevermind, it works! Thanks for the great work!

var doc = new jsPDF({
orientation: 'landscape',
unit: 'mm',
format: [100, 50]
})

@parallax parallax locked and limited conversation to collaborators Sep 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants