Create PDF from UIScrollView Objective-C

this question was previously posted ( Creating a PDF from a UIScrollView in an iphone application ), and the code I'm using is 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];
}

Customization: I have a UIScrollView, inside a UIView. I want to save the entire UIView (950,500px) and its in space (UIScrollView size) (520 500). The generated PDF file is only the size of a UIScrollView (520,500)

I read the answer, but it obviously changed the code, but it does not work for me. I tried to fix it all day.

I am a beginner, so please indicate everything that I should add to my question that I missed. Thank.

PS is an iPad app.

+3
source share
3 answers

- , , .

UIView UIScrollView, UIView, aView, self.

, UIView, , PDF ,

[self createPDFfromUIView:self saveToDocumentsWithFileName:UIViewPDF];

+1

, .

, PDF .

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];
+5

ScrollViewToPDF , , .

scrollview's layer renderInContext, PDF , , PDF PDF

. , scrollView

+1
source

All Articles