'change path for exported pdf
I have found this code over the internet for exporting the view to PDF. But the downloaded file stores in the library which will be hard to locate. How can I change the path, so that when I use the app on my iPhone, it gets stored in the download folder. Also, the pdf looks like it is a screenshot of the view, is there any way I can control the view to crop such as I don't want the navigation bar and other button to appear on the screen. Is there any way I can make it look like a document rather than a screenshot. Or may be resize the view to the point where I want. Or does anyone have a better code to export the file to PDF?
func exportToPDF() {
let outputFileURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("SwiftUI.pdf")
let pageSize = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
let rootVC = UIApplication.shared.windows.first?.rootViewController
//Render the PDF
let pdfRenderer = UIGraphicsPDFRenderer(bounds: CGRect(origin: .zero, size: pageSize))
DispatchQueue.main.async {
do {
try pdfRenderer.writePDF(to: outputFileURL, withActions: { (context) in
context.beginPage()
rootVC?.view.layer.render(in: context.cgContext)
})
print("wrote file to: \(outputFileURL.path)")
} catch {
print("Could not create PDF file: \(error.localizedDescription)")
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
