Post

Replies

Boosts

Views

Activity

PDFKit is not working in MacOs
I am creating a PDF document with the information from the database. For iOS I have it working completely, but for macOs I can't get it to work no matter what I try. I now have this code: func generateRentalPDF(_ invoiceModel: InvoiceModel, _ customerInfo: CustomerModel, _ completion: @escaping (Data?) -> Void) { guard let baseDocument = loadBasePDF(), let page = baseDocument.page(at: 0) else { completion(nil) return } var pageBounds = page.bounds(for: .mediaBox) let pdfData = NSMutableData() guard let dataConsumer = CGDataConsumer(data: pdfData as CFMutableData), let pdfContext = CGContext(consumer: dataConsumer, mediaBox: &pageBounds, nil) else { completion(nil) return } pdfContext.beginPDFPage(nil) if let cgPage = page.pageRef { pdfContext.drawPDFPage(cgPage) } // Set text attributes let textString = "Invoice Number: \(invoiceModel.invoiceNumber)" let textStyle = NSMutableParagraphStyle() textStyle.alignment = .left let textAttributes: [NSAttributedString.Key: Any] = [ .font: NSFont.systemFont(orSize: 18), .foregroundColor: NSColor.black, .paragraphStyle: textStyle ] // Calculate text position let textRect = CGRect(x: 100, y: pageBounds.height - 150, width: 300, height: 50) // Draw the text pdfContext.saveGState() pdfContext.translateBy(x: 0, y: pageBounds.height) pdfContext.scaleBy(x: 1.0, y: -1.0) let attributedText = NSAttributedString(string: textString, attributes: textAttributes) attributedText.draw(in: textRect) pdfContext.restoreGState() pdfContext.endPDFPage() pdfContext.closePDF() completion(pdfData as Data) } When I open the document in the app, I do see the base document. Even if I put a stroke around the text, I also see the stroke. However, the text does not appear on the screen. What am I overlooking or what am I doing wrong? I hope someone here has an answer for me, thank you very much in advance.
1
0
483
Dec ’23