Create ios-pdf in uiscrollview

in my ios app i am listing some data in scroll view. When you click the button, the data on the page is created in PDF format. The following is a snippet 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 can generate a PDF file, but I ran into the following problem. If I have more than 50 data, in my application I can only see the first 10 data and see the others that I need to scroll through. In this case, when the pdf file is created, it only accepts the data of the first 10, and not the others. How to get other data too

+2
source share
4 answers

renderInContext: will only display what you see at the moment - UIScrollView is intelligent and will only have the views in the hierarchy that you need right now. For manual scrolling and redrawing, you need a for-loop. It can be difficult to do it right, maybe call [aView layoutSubviews] manually after scrolling to make scrollView instantly reorder subviews.

+2
source

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

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

    1.UIGraphicsBeginPDFPage ()

    • get current context
    • renderIncontext

This method works correctly in my application, but I get with a big problem when the numOfLoop lager - you will slowly renderInContext - The memory will be enlarged → Close the application So how can I remove / release the context when I'm in one loop?

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

0
source

@steipete, @all Hi, I tried the code below, it generates a multi-page PDF file, but the visible area in the field of view is repeated for all pages. add more 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); } 
0
source

Check the ScrollViewToPDF example.

It uses the same scrollview's layer renderInContext , but here the PDF is created according to your requirement, for example, with one PDF page or several PDF pages

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

0
source

Source: https://habr.com/ru/post/1481308/


All Articles