Rudi
Rudi

Reputation: 4314

Generate PDF file more than one page in Xcode

I'm trying to generate a PDF file that have more than one page. I'm using this code :

-(void)createPDFfromUI:(UIWebView *)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

    UIWebView *myView = [[UIWebView alloc] init];

    [myView setBounds:CGRectMake(0,0, 320, 480)];

    UIGraphicsBeginPDFContextToData(pdfData, myView.bounds, nil);

    CGContextRef pdfContext = UIGraphicsGetCurrentContext();

    for (int i = 0; i < 4; i++) {

        UIGraphicsBeginPDFPage();

        aView.layer.bounds = CGRectMake(0,480* -i , 320, 480);

        [aView.layer renderInContext:pdfContext];

    }

    [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];
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];

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

The problem with this code is , it just generate a PDF file with 4 similar pages and they are all the last 480 px of my original page. (aView) Also when I move `[aView.layer renderInContext:pdfContext]; line out of the FOR loop it generates a PDF file with 4 pages but the first 3 pages will be blank and the last page has some data.

Do you guys have any idea how can i solve this problem ?!

Upvotes: 2

Views: 2281

Answers (2)

K-A
K-A

Reputation: 77

add string array and try refreshing the app

you must also add the physical pdf file to you xcode project you can do this by dragging and dropping it into the left hand menu browsing

another method could be used is accessing pdf remotley using URL

click run and see if it works

hope this helps

Upvotes: 0

Shahin
Shahin

Reputation: 885

bounds means the position of the layer in it's parent. so when you render a layer, it uses it's frame to determine it's contents. You have to place the view as a subview in another view such as aView2. then set the bounds of the aView just like you already did, then render aView2 instead.

It should work.

Upvotes: 1

Related Questions