Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pdfmake: How to create multiple page pdf with different orientation?

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
like image 249
Ismail Sunny Avatar asked Nov 04 '14 09:11

Ismail Sunny


3 Answers

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,
    }   
}
like image 51
RandomUser Avatar answered Nov 19 '22 17:11

RandomUser


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'}
   ]
}
like image 42
Isaac Avelar Avatar answered Nov 19 '22 18:11

Isaac Avelar


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.

like image 1
Vivek Pradhan Avatar answered Nov 19 '22 18:11

Vivek Pradhan