javascript pdfmake:如何创建具有不同方向的多页 pdf?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26731494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:24:42  来源:igfitidea点击:

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

javascriptnode.jspdfmake

提问by Ismail Sunny

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

有没有办法使用pdfmake创建具有不同页面方向的多页 pdf

to make it simple, I want something like this :

为简单起见,我想要这样的东西:

  • Page 1with portrait orientation
  • page 2with landscape orientation
  • page 3with portrait orientation
  • 纵向的第1
  • 2页横向
  • 3页,纵向

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

我用不同的方法尝试了很多次,但它总是对所有页面都有影响。



对不起我糟糕的英语

回答by 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 playgroundin order to try it.

在此行下方,我附上了一个简单的代码,您可以将其直接粘贴到pdfmake playground以进行尝试。

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

回答by Vivek Pradhan

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

我刚刚浏览了pdfmake的文档,他们提供了以下用于设置页面方向的代码:

 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 pageand the issues mentioned, I don't see a provision of setting page orientation for specific pages although you can add PageBreaks like so:

现在整个项目的本质是 Document 定义对象,我想这是独一无二的。即使在github 页面和提到的问题上,我也没有看到为特定页面设置页面方向的规定,尽管您可以像这样添加 PageBreaks:

  (...)
   '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 pdfdocument is generated in this project:

也就是说,我确实认为有一种解决方法可以解决您的问题。你看,pdf这个项目中的文档是这样生成的:

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

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

当然,管道fs模块是节点实现。但是,由于我们将页面方向附加到文档定义对象,如果我们有多个文档定义,如下所示:

 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 appendflag in the createWriteStream()method. Useful documentation. (Untested code)

我们现在可以appendcreateWriteStream()方法中使用标志。有用的文档。(未经测试的代码)

 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.

我只是建议您如何组合文档定义对象。希望它能让你朝着正确的方向开始。