I have a UICollectionView which scrolls through multiple pages. I want to put the entire collection view into PDF form, whereas now, all I get in PDF form is the part of the collection view that was last on the screen. The code is:
func createPdfFile() -> URL?
{
if let myView = myView {
let pdfData = NSMutableData()
UIGraphicsBeginPDFContextToData(pdfData, myView.bounds, nil)
UIGraphicsBeginPDFPage()
guard let pdfContext = UIGraphicsGetCurrentContext() else { return nil }
myView.layer.render(in: pdfContext)
UIGraphicsEndPDFContext()
let fileName = "temp.pdf"
let fileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileName)
if pdfData.write(to: fileURL, atomically: true) {
print("Successfully wrote to \(fileURL)")
return fileURL
}
else {
return nil
}
}
else {
DLog("Error: Could not create pdf")
return nil
}
}
func printView() {
if let fileURL = createPdfFile() {
let webView = UIWebView()
webView.loadRequest(URLRequest(url:
fileURL))
self.view = webView
}
else {
DLog("Error: Could not print the pdf. Write to disk failed.")
alertOk("Failed to generate PDF.", title: "Error")
}
}
How can I get it to render all of the view into a PDF?