Unable to create PDF and PRINT over 60 pages (memory rises and falls)

I need to create a PDF of more than 60 pages and you need to print it, but on iPhone and iPad the Ram memory rises to 350.50MB-500.00MB and it crashes.

To reduce memory -> Sending queues also does not help

Unable to find a solution for this. Plz help me on this ...

and the link below but doesn't help

Unable to create PDF document with more than 400 pages on iOS

-(NSData*)getPdfFullLineSheetiPhone:(UIScrollView *)tableView GridCount:(NSInteger)count{ // -- first page height, rest pages height: adjust to get it right #define FIRST_PAGE_HEIGHT_FULLSON 1040 #define REST_PAGES_HEIGHT_FULLSON 1090//1420 #define WIDTH_FULLSO_PORTRAITN 400 CGSize fittedSize; CGRect priorBounds = tableView.frame; // - the '200' is the cell height for estimating how many pages, and 200/3 is ROw calculation(How many rows in GMGridView) fittedSize =CGSizeMake(WIDTH_FULLSO_PORTRAITN, count * 200/3); tableView.bounds = CGRectMake(0, 0, fittedSize.width, fittedSize.height); 

Start creating page codes

 CGRect pdfPageBounds; // Standard US Letter dimensions 8.5" x 11" pdfPageBounds = CGRectMake(0, 0, 768/1.8, REST_PAGES_HEIGHT_FULLSON/1.79); NSMutableData *pdfData = [[NSMutableData alloc] init]; UIGraphicsBeginPDFContextToData(pdfData, pdfPageBounds, nil); int pageno=0; { // do page1 CGRect pdfPageBoundsPage1; pdfPageBoundsPage1 = CGRectMake(0,0,768/1.8, FIRST_PAGE_HEIGHT_FULLSON/1.7); UIGraphicsBeginPDFPageWithInfo(pdfPageBoundsPage1, nil); { CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 10, 0); [tableView.layer renderInContext:UIGraphicsGetCurrentContext()]; pageno ++; } //Rest of Pages for (CGFloat pageOriginY = FIRST_PAGE_HEIGHT_FULLSON/1.7; pageOriginY < fittedSize.height; pageOriginY += REST_PAGES_HEIGHT_FULLSON/1.79) { @autoreleasepool { UIGraphicsBeginPDFPageWithInfo(pdfPageBounds, nil); { CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 10, -pageOriginY); [tableView.layer renderInContext:UIGraphicsGetCurrentContext()]; pageno ++; } } } } UIGraphicsEndPDFContext(); tableView.bounds = priorBounds; return pdfData; } 

Memory rises in the iPad4, while the iPad Mini crashes 180-240MB nd enter image description here

+1
source share
2 answers

you need to create your code something like this:

 UIGraphicsBeginPDFContextToFile( pdfPath, CGRectZero, nil );// as per rMaddy UIGraphicsBeginPDFPageWithInfo(); CGContextRef pdfContext = UIGraphicsGetCurrentContext(); [tableView.layer renderInContext:pdfContext]; UIGraphicsEndPDFContext(); 

here: The namecan file will look like this:

 NSString *newPDFName = [NSString stringWithFormat:@"%@.pdf", @"whatEverNameYouWant"]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:newPDFName]; NSLog(@"%@",pdfPath); 

basically the main advantage of this approach will be the reduction of NSData, which creates memory pressure. above all the code will look something like this:

 // Set up we the pdf we're going to be generating is UIGraphicsBeginPDFContextToFile(pdfPath, CGRectZero, nil); int i = 0; for ( ; i < pages; i++) { @autoreleasepool{ // Specify the size of the pdf page UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kDefaultPageWidth, kDefaultPageHeight), nil); CGContextRef currentContext = UIGraphicsGetCurrentContext(); // Move the context for the margins CGContextTranslateCTM(currentContext, kMargin, kMargin); // draw the layer to the pdf, ignore the "renderInContext not found" warning. [tableView.layer.layer renderInContext:currentContext]; } } // all done with making the pdf UIGraphicsEndPDFContext(); 

Here it is! you can take care of your calculations

+1
source

This is not the answer to this question, this is another code that was used to create PDf using

UIGraphicsBeginPDFPageWithInfo. This approach also falls in more than 500 rows, but it is about 56 pages

After this approach, after returning the PDF data, when I assign this PDF data to the UIPrinterInteractionController action -

It shows SO, I can not calculate Pages

 Print-Job failed: Printer exists. 2016-05-27 00:37:26.131 APPName[9078:2952235] \032Send\032to\032Mac\ 032@ \032macminiB._ipp._tcp.local.: startJob not called. 

Note: while this printer error does not appear in the above code, which I posted above using UIGraphicsBeginPDFContextToData p>

 -(NSData *)getPdfSimpleSOTr:(UITableView *)tableView{ #define FIRST_PAGE_HEIGHT 1188 #define REST_PAGES_HEIGHT 1176.5 CGSize fittedSize; CGRect priorBounds; // 140208 dan - Comment: save the WIDTH CGRect savedFrame = tableView.frame; // 140207 dan - force portrait width priorBounds = tableView.frame; priorBounds.size.width=768; // put into Portrait tableView.frame = priorBounds; fittedSize = [tableView sizeThatFits:CGSizeMake(priorBounds.size.width, ([tableView numberOfRowsInSection:0] * 49) + 529)]; tableView.bounds = CGRectMake(0, 0, fittedSize.width, fittedSize.height); CGRect pdfPageBounds; pdfPageBounds = CGRectMake(0, -12, 768, REST_PAGES_HEIGHT); 

File name and path

 NSString *newPDFName = [NSString stringWithFormat:@"%@.pdf", @"AppName"]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:newPDFName]; NSLog(@"%@",pdfPath); 

Page creation

 // Set up we the pdf we're going to be generating is UIGraphicsBeginPDFContextToFile(pdfPath, CGRectZero, nil); int pageno=0; { CGRect pdfPageBoundsPage1 = CGRectMake(0,0,768, FIRST_PAGE_HEIGHT+15);//15 UIGraphicsBeginPDFPageWithInfo(pdfPageBoundsPage1, nil); { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(context, 10, 0); [tableView.layer renderInContext:context]; pageno ++; } for (CGFloat pageOriginY = FIRST_PAGE_HEIGHT; pageOriginY < fittedSize.height; pageOriginY += REST_PAGES_HEIGHT) { @autoreleasepool{ // Specify the size of the pdf page UIGraphicsBeginPDFPageWithInfo(pdfPageBounds, nil); CGContextRef context = UIGraphicsGetCurrentContext(); // Move the context for the margins CGContextTranslateCTM(context, 10, -pageOriginY); // draw the layer to the pdf, ignore the "renderInContext not found" warning. [tableView.layer renderInContext:context]; } } } // all done with making the pdf UIGraphicsEndPDFContext(); 

After GraphicsEnd will extract NSData from FilePath

  NSData *pdfData; if([[NSFileManager defaultManager] fileExistsAtPath:pdfPath]) { pdfData = [[NSFileManager defaultManager] contentsAtPath:pdfPath]; } else { NSLog(@"File not exits"); } tableView.bounds = priorBounds; // 140208 dan - Comment: restored the saved WIDTH tableView.frame=savedFrame ; return pdfData; } 
0
source

All Articles