15

is there a way to create multiple page pdf with different page orientation using pdfmake?

to make it simple, I want something like this :

  • Page 1 with portrait orientation
  • page 2 with landscape orientation
  • page 3 with portrait orientation

I've tried it so many times with different method but it always effect to all page.


sorry for my terible english
Ismail Sunny
  • 151
  • 1
  • 1
  • 3

3 Answers3

20

This is and old question, but answering may help others.

If you want to change page orientation you only need specify the new value at the first node of the page.

Below this lines I have attached a simple code that you can paste directly at pdfmake playground in order to try it.

Good luck!

var dd = {
    content: [
        { 
            text: 'Unordered list', 
            style: 'header' 
        },
        {
            ol: [
                'item 1',
                'item 2',
                'item 3',
            ]
        },
        { 
             text: '\n\nUnordered list with longer lines', 
             style: 'header', 
             pageBreak: 'before', 
             pageOrientation: 'landscape' 
        },
        {
            ol: [
                'item 1',
                'Lorem ipsum dolor sit amet, consectetur ..',
                'item 3',
            ]
        },
        { 
            text: '\n\nNested lists', 
            style: 'header', 
            pageBreak: 'before', 
            pageOrientation: 'portrait' 
        },
        {
            ol: [
                'item 1',
                'Lorem ipsum dolor sit amet, consectetur ..',
                {
                    ol: [
                        'subitem 1',
                        'subitem 2',
                        'subitem 3 - Lorem ipsum dolor sit ame...',
                        'subitem 4',
                        'subitem 5',
                    ]
                },
                'item 3\nsecond line of item3',
            ]
        },
    ],
    styles: {
        header: {
            bold: true,
            fontSize: 15
        }
    },
    defaultStyle: {
        fontSize: 12,
    }   
}
jedi
  • 839
  • 12
  • 33
RandomUser
  • 1,094
  • 1
  • 12
  • 24
  • How i can use this method if custruct the pdf dinamically in a for? – jedi Oct 09 '15 at 10:26
  • 1
    adding every object to the array `content` with the `push` function, and divide every page at the end of the for cycle whit the pagebreak option. `var foo[0]={ name:"hello" }; foo[1]={ name:"world" }; for(var i=0;i – jedi Oct 09 '15 at 16:25
2

you can try this in pdf make

var dd = { 
pageOrientation: 'portrait',
content: [
   {text: 'Text on Portrait'},
   {text: 'Text on Landscape', pageOrientation: 'landscape', pageBreak: 'before'},
   {text: 'Text on Landscape 2', pageOrientation: 'portrait', pageBreak: 'after'},
   {text: 'Text on Portrait 2'}
   ]
}
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 06 '21 at 17:58
1

I was just going through the documentation for pdfmake where they give the following code for setting page orientation:

 var docDefinition = {
  // a string or { width: number, height: number }
  pageSize: 'A5',  
 // by default we use portrait, you can change it to landscape if you wish
 pageOrientation: 'landscape',
  ...

//Other content
};

Now the essence of this entire project is the Document definition object which is unique I guess. Even on the github page and the issues mentioned, I don't see a provision of setting page orientation for specific pages although you can add PageBreaks like so:

  (...)
   'You can also fit the image inside a rectangle',
  {
    image: 'fonts/sampleImage.jpg',
    fit: [100, 100],
    pageBreak: 'after'
  },
  (...)

That said, I do think there is a workaround for your problem. You see, this is how the pdf document is generated in this project:

 var fs = require('fs');
 var pdfDoc = printer.createPdfKitDocument(docDefinition);
 pdfDoc.pipe(fs.createWriteStream('pdfs/basics.pdf'));
 pdfDoc.end();

Ofcourse the pipe and fs modules are node implementations. However, since we have page orientation attached to a document definition object, if we have multiple doc definitions like so:

 var pdf1 = printer.createPdfKitDocument(docdef1); //landscape mode page 1
 var pdf2 = printer.createPdfKitDocument(docdef2); //portrait mode page 2
 var pdf3 = printer.createPdfKitDocument(docdef3); //landscape mode for the rest of the pages.

We can now just use the append flag in the createWriteStream() method. Useful documentation. (Untested code)

 pdf1.pipe(fs.createWriteStream('foo.pdf'));
 pdf2.pipe(fs.createWriteStream('foo.pdf',{flags:'a'}));
 pdf3.pipe(fs.createWriteStream('foo.pdf',{flags:'a'}));
 pdf1.end();
 pdf2.end();
 pdf3.end();

I was just suggesting how you might go about combining document definition objects. Hope it gets you started in the right direction.

Vivek Pradhan
  • 4,777
  • 3
  • 26
  • 46
  • thank you for your respond, I will try your suggestion right away and inform you with the result. I just want to make it sure, is your method create 3 pdf or just 1 pdf? btw, My goal is create 1 pdf with different orientation – Ismail Sunny Nov 04 '14 at 23:39
  • I see your point, you tried to combine 3 generated pdf into 1 pdf file. I've tried it, but it's only display the first page. :( ` – Ismail Sunny Nov 05 '14 at 01:02