'renderer.writePDF doesn't work without error
I learn swift and faced with problem: when i try write pdf file in document directory nothing happened. Code:
func createPDF() {
// MARK: - create path
let documentDir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
let path = documentDir.appendingPathComponent("\(clientCompanyName + UUID().uuidString).pdf")
// MARK: - create meta
let pdfMetaData = [
kCGPDFContextCreator: "something",
kCGPDFContextAuthor: "https://bla-bla.com"
]
let format = UIGraphicsPDFRendererFormat()
format.documentInfo = pdfMetaData as [String: Any]
// MARK: - set page size
let pageWidth = 8.5 * 72.0
let pageHeight = 11 * 72.0
let pageRect = CGRect(x: 0, y: 0, width: pageWidth, height: pageHeight)
// MARK: - render
let renderer = UIGraphicsPDFRenderer(bounds: pageRect, format: format)
try? renderer.writePDF(to: path) { (context) in
let attributes = [
NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 72)
]
let text = clientCompanyName + " " + formattedDate
text.draw(at: CGPoint(x: 0, y: 0), withAttributes: attributes)
}
}
When i start this code, i don't see any errors in terminal. But if i open Files, my document didn't save
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
