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

这一生的挚爱 提交于 2019-12-01 04:18:05
RandomUser

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,
    }   
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!