techprimate / TPPDF

TPPDF is a simple-to-use PDF builder for iOS and macOS written in Swift

Home Page:https://techprimate.github.io/TPPDF/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Background is not generated if there is no text

Dark-Alone opened this issue · comments

The example code does not work if you comment out the part that adds text to the group.
Is it possible to display and generate a background shape without adding text?

@Observable
class PDFWorker {
    var url: URL
    
    init() {
        let document = PDFDocument(format: .a4)
        
        // Create sample bezier path
        let size = CGSize(width: 100, height: 100)
        let path = PDFBezierPath(ref: CGRect(origin: .zero, size: size))
        path.move(to: PDFBezierPathVertex(position: CGPoint(x: size.width / 2, y: 0), anchor: .topCenter))
        path.addLine(to: PDFBezierPathVertex(position: CGPoint(x: size.width, y: size.height / 2),
                                             anchor: .middleRight))
        path.addLine(to: PDFBezierPathVertex(position: CGPoint(x: size.width / 2, y: size.height),
                                             anchor: .bottomCenter))
        path.addLine(to: PDFBezierPathVertex(position: CGPoint(x: 0, y: size.height / 2),
                                             anchor: .middleLeft))
        path.close()
        
        // Create a dynamic shape using the defined cache in reference to its size
        let shape = PDFDynamicGeometryShape(path: path, fillColor: .orange, stroke: .none)
        
        // Create the group object and set the background color and shape
        let group = PDFGroup(allowsBreaks: false,
                             backgroundColor: .none,
                             backgroundShape: shape,
                             padding: EdgeInsets(top: 50, left: 50, bottom: 50, right: 180))

//        for i in 0..<10 {
//            group.set(font: Font.systemFont(ofSize: 18))
//            group.set(indentation: 30 * CGFloat(i % 5), left: true)
//            group.set(indentation: 30 * CGFloat(i % 3), left: false)
//            group.add(text: "0")
//        }
        document.add(group: group)
        
        let generator = PDFGenerator(document: document)
        self.url = try! generator.generateURL(filename: "example2.pdf")
        print("generated")
    }
}

I looked into it and this behaviour is expected, because the text content gives the PDFGroup the intrinsic size, which is used as the frame of the background shape.

Currently it is not possible to add PDFDynamicGeometryShape directly to a PDFDocument, simply because it was not requested yet.

What you could try is using group.add(space: 100) to add empty content to the group.

Closed due to inactivity. Feel free to reopen when relevant again