Create PDF from UIScrollView Objective C

2019-04-11 18:38发布

问题:

this question has been posted before (Creating PDF from UIScrollView in iphone app) and the code I am using is from here.

Here is the code

-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
    // Creates a mutable data object for updating with binary data, like a byte array
    NSMutableData *pdfData = [NSMutableData data];

    // Points the pdf converter to the mutable data object and to the UIView to be converted
    UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
    UIGraphicsBeginPDFPage();
    CGContextRef pdfContext = UIGraphicsGetCurrentContext();


    // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData

    [aView.layer renderInContext:pdfContext];

    // remove PDF rendering context
    UIGraphicsEndPDFContext();

    // Retrieves the document directories from the iOS device
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

    NSString* documentDirectory = [documentDirectories objectAtIndex:0];
    documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];

    // instructs the mutable data object to write its context to a file on disk
    [pdfData writeToFile:documentDirectoryFilename atomically:YES];
}

Setup: I have a UIScrollView, inside is a UIView. I want to save the entire UIView (950,500px) and it's in a space (the UIScrollView size) of (520,500). The PDF being generated is only the size of UIScrollView (520,500)

I read the answer but he apparently changed the code but it doesn't work for me. I've been trying to fix this all day.

I'm a beginner so please indicate anything I should add to my question that I missed. Thank you.

PS - this is an iPad app.

回答1:

The context should have the size of the scrollview's content size, not the bounds.

Then you need to temporarily resize the scrollview to its content size, render it in the PDF context, and restore the size of the scrollview to its original size.

UIGraphicsBeginPDFContextToData(pdfData, (CGRect){0,0, scrollView.contentSize}, nil);

CGRect origSize = scrollView.frame;
CGRect newSize = origSize;
newSize.size = scrollView.contentSize;
[scrollView setFrame:newSize];

[scrollView.layer renderInContext:pdfContext];

[scrollView setFrame:origSize];


回答2:

Check ScrollViewToPDF example and you will understand what you need to do.

It uses same scrollview's layer renderInContext but here PDF is created according to your requirement such as one page PDF or multiple page PDF

Note : It captures all visible as well as invisible part of scrollView



回答3:

If anyone was wondering, I found how to fix this.

Since my UIView is IN a UIScrollView, and I use another class for the UIView, when I call the method and chose the parameter aView, I just put in self.

So in my UIView class, when I want the PDF to be generated I type in

[self createPDFfromUIView:self saveToDocumentsWithFileName:UIViewPDF];