javascript - 动态检查文本是否转到下一页并使用 pdfmake 在 pdf 中添加分页符

标签 javascript pdf pdfmake

对于我使用 javascript 中的 pdfmake 即时制作报价和发票 pdf 的项目。我面临的问题是文本 block 在中间离开页面。我想要的是检查某个文本 block 或表格是否要在页面之间拆分,如果是,则在该 block 之前添加分页符以确保文本或表格完全在一页上。

我的 pdf 文档定义是这样构建的:

return {
                content: [
                    getOfferLogo(), //Get the logo or empty string
                    getHeading(), //get the customer and business data (adress etc)
                    //the above is always the same
                    getText(), //get the textblock, created by user and always different
                    getSpecifics(), //get a table of payment specifications
                    getSignature() //get last textblock contaning signature fields etc, always the same
                ],
                styles: {
                    subheader: {
                        fontSize: 15,
                        bold: true,
                        alignment: 'center'
                    }
                },
                defaultStyle: {
                    columnGap: 20,
                    fontSize: 12
                }
            };

简而言之,我如何在创建 pdf 之前检查文本是否会离开页面并相应地添加分页符?

提前致谢。

最佳答案

pageBreakBefore 函数在确定是否需要分页符方面提供了很大的灵 active 。但是,我找到了另一种解决方案,它更直接且记录更少,但会自动完成所有魔术。这是 0.1.32 中的 unbreakable: true 属性发布。此外,它在以下线程中提到 https://github.com/bpampuch/pdfmake/issues/1228#issuecomment-354411288

它是如何运作的? 例如,您想使标题及其下方的一些文本牢不可破。为此,您必须将 header 和内容包装在一个堆栈中,并在其上应用 unbreakable: true

{
    stack: [
        // header
        {
            text: 'Lorem ipsum dolor sit amet',
            bold: true,
            fontSize: 13
        },
        // content
        {
            text: 'Nulla iaculis magna vitae luctus euismod. Sed arcu risus, mattis non molestie et, condimentum sit amet justo. Quisque vitae neque magna. Etiam in tellus vitae arcu volutpat bibendum. In ullamcorper ante tortor, a viverra libero cursus eu. Phasellus quis massa nec lorem feugiat ultricies. Aliquam erat volutpat. Nullam a purus tempus, feugiat elit vel, tincidunt tortor.'
        }
    ],
    unbreakable: true // that's the magic :)
}

关于javascript - 动态检查文本是否转到下一页并使用 pdfmake 在 pdf 中添加分页符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33996534/

相关文章:

pdf - (DOORS/DXL) 导出为 pdf 或使用给定模板打印

javascript - PdfMake 要重复的项目列表(文本旁边的复选框)

javascript - pdfMake无法读取未定义的属性 '_calcWidth'

javascript - 在 JavaScript 中装饰实例方法时出现问题

android - 在 Android 中生成并打印具有特定大小的 PDF

javascript - 使用 Google Feeds API 访问远程 RSS 提要

ios - 在 UIWebView 中加载 PDF 时出现内存警告

javascript - pdfmake.js 中表格的线宽

javascript - jQuery基于active切换侧边栏子菜单

javascript - 未捕获的 TypeError : Cannot read property 'appendChild' of null. 在我的代码中找不到错误