dineshprasanna
dineshprasanna

Reputation: 1284

ios- pdf generation in uiscrollview

in my ios app i am listing out some data in scroll view. When a button is clicked the datas in the page are been generated in pdf file format. Following is the bit of 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];
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];

    // instructs the mutable data object to write its context to a file on disk
    [pdfData writeToFile:documentDirectoryFilename atomically:YES];
    NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
}

From the above code, i am able to generate a pdf file, but i am facing the following issue. If i have more than 50 data, in my app i am able to see only the first 10 data and to see the others i need to scroll them. In this case when the pdf file gets generated it is taking only the data of first 10 and not the others. How to get the other data too

Upvotes: 2

Views: 2043

Answers (4)

Paresh Navadiya
Paresh Navadiya

Reputation: 38259

Check ScrollViewToPDF example

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

Upvotes: 0

kart
kart

Reputation: 45

@steipete, @all Hi i tried the below code it generate multipage PDF, but the visible area in view is repeated for all the pages. please add any further ideas to improve the result.

-(void)createPDFfromUIView:(UIWebView*)aView saveToDocumentsWithFileName:(NSString*)aFilename

{

NSString *oldFilePath= [[NSBundle mainBundle] pathForResource:@"ABC" ofType:@"pdf"];

NSMutableData *pdfData = [NSMutableData data];

UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil); 

CFURLRef url = CFURLCreateWithFileSystemPath (NULL, (CFStringRef)oldFilePath, kCFURLPOSIXPathStyle, 0); 

CGPDFDocumentRef templateDocument = CGPDFDocumentCreateWithURL(url); 

CFRelease(url); size_t count = CGPDFDocumentGetNumberOfPages(templateDocument); 

for (size_t pageNumber = 1; pageNumber <= count; pageNumber++) 

{ 

UIGraphicsBeginPDFPage(); 

CGContextRef pdfContext = UIGraphicsGetCurrentContext(); 

[aView.layer renderInContext:pdfContext]; 

}

 CGPDFDocumentRelease(templateDocument); 

// 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];

 [pdfData writeToFile:documentDirectoryFilename atomically:YES];

NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);

}

Upvotes: 0

Paul Nguyen
Paul Nguyen

Reputation: 13

as steipete said, you can use loop to renderIncontext - You need to calculate num of loop numOfLoop = webview.ScrollView.ContentSize.Height/weview.frame.size.height

  • use loop to renderInContext for(int i = 0; i < numOfLopp; i ++)

    1.UIGraphicsBeginPDFPage()

    1. get current context
    2. renderIncontext

This way run correct in my app but I am getting with BIG issue when the numOfLoop lager - you will get slow to renderInContext - Memory will be increase -> Close app In this way, How can I remove/release context when I am in once loop ?

Other way, you can use https://github.com/iclems/iOS-htmltopdf/blob/master/NDHTMLtoPDF.h to generate to PDF

Upvotes: 0

steipete
steipete

Reputation: 7641

renderInContext: will only render what you currently see - UIScrollView is smart and will only have the views in the hierarchy that you need right now. You need a for-loop to manually scroll and redraw. This might be tricky to get right, maybe call [aView layoutSubviews] manually after scrolling to force the scrollView to rearrange the subviews instantly.

Upvotes: 2

Related Questions