Question & Answer: onView` 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…..

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?

Expert Answer

 

You are printing only last view to pdf. To render all views we need to iterate all views and then generate pdf.

func createPdfFile() -> URL?

{

if let myView = myView {

int restorer = mineTable.frame.size.height;

CGRect _tmp = CGRectMake(mineTable.frame.origin.x, mineTable.frame.origin.y, mineTable.frame.size.width, 398*formDataStorage.cake.count);

[self resizeTheTable:_tmp];

int add_num = _tmp.size.height – _tmp.origin.y;

CGRect media_box = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height + add_num);

NSMutableData *data = [NSMutableData data];

CGSize currPageSize = CGSizeMake(self.view.frame.size.width, 792);

UIGraphicsBeginPDFContextToData(data, CGRectZero, nil);

CGContextRef currPdfContext = UIGraphicsGetCurrentContext();

NSInteger presentPage = 0;

BOOL isCompleted = NO;

do {

UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0.0, currPageSize.width, currPageSize.height), nil);

CGContextTranslateCTM(currPdfContext, 0, -(currPageSize.height*presentPage));

[self.view.layer renderInContext:currPdfContext];

if ((currPageSize.height*(presentPage+1)) > media_box.size.height) isCompleted = YES;

else presentPage++;

} while (!isCompleted);

UIGraphicsEndPDFContext();

[data writeToFile:URL atomically:YES];

NSLog(@”PDF rendered to given filename : %@”, URL);

}

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”)

}

}

Still stressed from student homework?
Get quality assistance from academic writers!