从HTML模板在Swift中生成PDF

时间:2018-03-08 20:11:29

标签: html ios css swift

我正在尝试从HTML-CSS模板为我的iOS应用程序实现PDF生成器。

它成功生成了它,虽然由于某种原因,它没有在页面末端正确剪切。 Screenshot from PDF export

我已经尝试过了;

我的PrintRenderer Swift类是;

class CustomPrintPageRenderer: UIPrintPageRenderer {

    let A4PageWidth: CGFloat = 595.2
    let A4PageHeight: CGFloat = 841.8

    override init() {
        super.init()

        let pageFrame = CGRect(x: 0.0, y: 0.0, width: A4PageWidth, height: A4PageHeight)

        self.setValue(NSValue(cgRect: pageFrame), forKey: "paperRect")
        self.setValue(NSValue(cgRect: pageFrame.insetBy(dx: 10, dy: 10)), forKey: "printableRect")

    }
}

我的HTML模板文件&#39> media-print 部分是;

        @media print {
            height: auto;

            #skills {
                padding-top: 5mm;
                display: block;
                page-break-before: auto;
                page-break-inside: avoid;
                page-break-after: auto;

                background-color: red;
            }

            #lesson {
                height: 100%;
                display: block;

                page-break-before: avoid;
                page-break-inside: always;
                page-break-after: auto;
                border-top: none;
                background-color: green;
            }

            #drivingTests {
                display: block;

                page-break-before: auto;
                page-break-inside: auto;
                page-break-after: avoid;
                border-top: none;
                background-color: yellow;
            }

            .wrapTable:not(.innerWrapTable) {
                display: block;
                page-break-before: auto;
                page-break-inside: auto;
                page-break-after: auto;
            }


        }

HTML模板文件的正文是;

<body>
    <div id="contentWrap">

        <!-- Header -->
        <header>
            <table>
                <tr>
                    <td>
                        <table>
                            <tr>
                                <td>
                                    <p>Ausbildungskarte</p>
                                </td>
                                <td>
                                    <p>Kat. #category#</p>
                                </td>
                            </tr>
                        </table>
                    </td>
                    <td>#headerLogo#</td>
                </tr>
            </table>
        </header>

        <!-- Personal Information -->
        <table id="personalInformation" class="wrapTable">
            <tr>
                <th class="headers">Persönliche Informationen</th>
            </tr>

                #personalInformationItems#
        </table>

        <!-- Skills -->
        <table id="skills" class="wrapTable">
            <tr>
                <th class="headers">Skills</th>
            </tr>
            <tr>
                <td>
                    <table class="wrapTable innerWrapTable">
                        <tr>
                            <td class="skillColumnItem">#skillSectionLeftItem#</td>
                            <td class="skillColumnItem">#skillSectionMiddleItem#</td>
                            <td class="skillColumnItem">#skillSectionRightItem#</td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>

        <!-- Lesson -->
        <table id="lesson" class="wrapTable">
            <tr>
                <th class="headers">Lessons</th>
            </tr>

                #lessonItems#
        </table>

        <!-- Driving Tests -->
        <table id="drivingTests" class="wrapTable">
            <tr>
                <th class="headers">Driving Tests</th>
            </tr>

                #drivingTestItems#
        </table>
    </div>
</body>

如有必要,我可以共享完整的HTML文件,大约300行。

感谢任何建议或纠错。

0 个答案:

没有答案