Automate beginPage() for PDF using CoreGraphics in Swift 5 Xcode

In my app, I want to print large, paragraph-length Strings onto a PDF (like if you wrote a huge paper in Microsoft Word and then printed it to a PDF). Where I'm struggling is in figuring out how to make it know when to call beginPage() and bleed onto a new page (at a certain distance down the page)? For example, imagine opening a Word Document and typing until the first page is completely full, then notice how it automatically goes to a new page when necessary. I would imagine Xcode would need to know where to print down to (pageBottom + certain amount of pixels off the bottom, then it would need to cut the rest of the String still needing to be printed, create a new page, know where to start printing at, then continue printing the string where it left off, on the second page.

Essentially:

//1
let paragraphFont = UIFont.systemFont(ofSize: 12.0, weight: .regular)
//2
let paragraphAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: paragraphFont]
//3
let attributedParagraph = NSAttributedString(
        string: "\(paragraphToBePrinted)",
        attributes: paragraphAttributes
    )
//4
let paragraphStringSize = attributedParagraph.size()
//5
let paragraphStringRect = CGRect(
        x: (pageRect.width - pageRect.width) + 20,
        y: paragraphTop,
        width: paragraphStringSize.width,
        height: paragraphStringSize.height
    )
//6
attributedParagraph.draw... if while printing the paragraph you come to a certain point near the bottom 
of the page (pageBottom + 50), then create a new page, and continue printing at startPoint (pageTop - 50).

Any ideas? I'm using CoreGraphics to create the PDF and PDFKit to save it.

Here's where I learned this code from:

Thanks!

You will probably have better luck, or, at least have more people with experience with all the Apple-private frameworks you are invoking if you look at an Apple-specific developer site like the Apple Developer forums. These forums are really about the open-source Swift language and associated open-source libraries (although SwiftUI and Combine seem to be getting a pass even though they are Apple-private).

I assume when you say "Xcode" in your question, you are really referring to the Apple frameworks like Core Graphics and Core Text. Xcode is simply a build environment and itself has nothing to do with a running app, unless you are debugging it. Your app will most likely have to keep track of where page breaks are going to be inserted based on margin specifications and page size, using Core Text where necessary.

Idk why I put Xcode, I guess I just did it more for a tag to aid anyone Googling the same issue. I'm using CoreGraphics to create the PDF and PDFKit to save it.

I’ll drop it over there as well, thanks for the recommendation.

The page breaks and all that is I guess what I’m trying to figure out.

Edit: So far so one has responded over there either... same for StackOverflow. I guess PDF handling isn't common.

Conceptually this is straightforward but there are a lot of details.

Given a font and column width there is an API that tells you the rectangle that a given string will fit into. You walk down the page accumulating the height of the paragraphs until you reach one the goes over the page height. You then nibble words off the end of that string until it fits on the page. Then start a new page with the remainder of the last paragraph.

I've written this code, in Obj-C, but don't have it handy.

1 Like
Terms of Service

Privacy Policy

Cookie Policy