问题
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
回答1:
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,
}
}
回答2:
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.
来源:https://stackoverflow.com/questions/26731494/pdfmake-how-to-create-multiple-page-pdf-with-different-orientation