在 Swift 中为 PDFKit 添加分页符 html

Add a page break in html for PDFKit in Swift

我正在使用 HTML 和 PDFKit 创建一个 PDF,如下所示:


let contextString = "<p style=\"page-break-after: always;\">hello</p><p>This is a test</p>"

let print = UIMarkupTextPrintFormatter(markupText: contextString)

let render = UIPrintPageRenderer()
render.addPrintFormatter(print, startingAtPageAt: 0)

let page = CGRect(x: 0, y: 0, width: 595.2, height: 841.8) // A4, 72 dpi
render.setValue(page, forKey: "paperRect")
render.setValue(page, forKey: "printableRect")

let pdfData = NSMutableData()
UIGraphicsBeginPDFContextToData(pdfData, .zero, nil)

for i in 0..<render.numberOfPages {
UIGraphicsBeginPDFPage();
    render.drawPage(at: i, in: UIGraphicsGetPDFContextBounds())
}
UIGraphicsEndPDFContext();

let av = UIActivityViewController(activityItems: [pdfData], applicationActivities: nil)

UIApplication.shared.windows.first?.rootViewController?.present(av, animated: true, completion: nil)

我在第一个 <p> 之后添加了一个分页符。这有点效果。它有两个分页符,第二个 <p> 根本不显示。

有人有这方面的经验吗?
如何在 PDF 中添加分页符?

只需对 contextString 进行一次更改,我就能让您的代码正常工作。

通过将其更改为 page-break-before 而不是 page-break-after 它似乎有效。这就是我将您的 contextString 更改为的内容,我使用了多行字符串,因为它更易于阅读并提供了更清晰的内容。

let contextString = """
<p>Content in page 1</p>
<p style=\"page-break-before: always;\"></p>
<p>Content in page 2</p>
"""

这是一个非常简单的示例,您可以将其放入 Xcode。

class ViewController: UIViewController {

    let button = UIButton()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.addSubview(button)
        button.translatesAutoresizingMaskIntoConstraints = false

        button.setTitle("Print", for: .normal)
        button.setTitleColor(.systemBlue, for: .normal)
        button.addTarget(self, action: #selector(tapped), for: .touchUpInside)

        NSLayoutConstraint.activate([
            button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            button.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            button.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
        ])
        view.backgroundColor = .systemBackground
    }

    @objc func tapped() {

        let contextString = """
        <p>Content in page 1</p>
        <p style=\"page-break-before: always;\"></p>
        <p>Content in page 2</p>
        """

        let print = UIMarkupTextPrintFormatter(markupText: contextString)


        let render = UIPrintPageRenderer()
        render.addPrintFormatter(print, startingAtPageAt: 0)

        let page = CGRect(x: 0, y: 0, width: 595.2, height: 841.8) // A4, 72 dpi
        render.setValue(page, forKey: "paperRect")
        render.setValue(page, forKey: "printableRect")

        let pdfData = NSMutableData()
        UIGraphicsBeginPDFContextToData(pdfData, .zero, nil)

        for i in 0..<render.numberOfPages {
        UIGraphicsBeginPDFPage();
            render.drawPage(at: i, in: UIGraphicsGetPDFContextBounds())
        }
        UIGraphicsEndPDFContext();

        let av = UIActivityViewController(activityItems: [pdfData], applicationActivities: nil)

        self.present(av, animated: true, completion: nil)
    }
}

这是它的 gif,其中显示了带有文本的两个页面: