Add page number on bottom of each PDF page

Discussion in 'iOS Programming' started by fradiocey, Nov 29, 2015.

  1. fradiocey macrumors newbie

    Joined:
    Nov 29, 2015
    #1
    Hi,

    Anyone here have use this NDHTML to PDF before, it works perfectly, but how to add page number of each PDF file,I manage to figure NSLog but I don't have any clues to put at the PDF canvas. Im new in objective-c, your help is really appreciated,

    Thank You.


    References:

    https://github.com/iclems/iOS-htmltopdf
    https://github.com/moderna/cordova-plugin-html2pdf


    Code:
    [*]#import"Html2pdf.h"
    [*]@interface Html2pdf (Private) 
    [*]- (BOOL) saveHtml:(NSString*)html asPdf:(NSString*)filePath; 
    [*]@end
    [*]@interface UIPrintPageRenderer (PDF) 
    [*]- (NSData*) printToPDF; 
    [*]@end
    [*]@implementation Html2pdf 
    [*]@synthesize command, filePath, pageSize, pageMargins, documentController; 
    [*]- (void)create:(CDVInvokedUrlCommand*)command 
    [*]{ 
    [*] self.command = command; 
    [*] NSArray* arguments = command.arguments; 
    [*] NSLog(@"Creating pdf from html has been started."); 
    [*] NSString* html = [arguments objectAtIndex:0]; 
    [*] self.filePath = [[arguments objectAtIndex:1] stringByExpandingTildeInPath]; 
    [*] / 
    [*] NSString* wwwFilePath = [[NSBundle mainBundle] pathForResource:@"www" ofType:nil]; 
    [*] NSURL* baseURL = [NSURL fileURLWithPath:wwwFilePath]; 
    [*] / 
    [*] self.pageSize = kPaperSizeA4; 
    [*] self.pageMargins = UIEdgeInsetsMake(10, 40, 50, 40); 
    [*] / 
    [*] UIWebView* webPage = [[UIWebView alloc] init]; 
    [*] webPage.delegate = self; 
    [*] webPage.frame = CGRectMake(0, 0, 1, 1); / 
    [*] webPage.alpha = 0.0; / 
    [*] [self.webView.superview addSubview:webPage]; 
    [*] [webPage loadHTMLString:html baseURL:baseURL]; 
    [*]} 
    [*]- (void)success 
    [*]{ 
    [*] NSString* resultMsg = [NSString stringWithFormat:@"HTMLtoPDF did succeed (%@)", self.filePath]; 
    [*] NSLog(@"%@",resultMsg); 
    [*] / 
    [*] CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK 
    [*] messageAsString:[resultMsg stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
    [*] / 
    [*] [self writeJavascript:[result toSuccessCallbackString:command.callbackId]]; 
    [*]} 
    [*]- (void)error:(NSString*)message 
    [*]{ 
    [*] NSString* resultMsg = [NSString stringWithFormat:@"HTMLtoPDF did fail (%@)", message]; 
    [*] NSLog(@"%@",resultMsg); 
    [*] / 
    [*] CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR 
    [*] messageAsString:[resultMsg stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
    [*] / 
    [*] [self writeJavascript:[result toErrorCallbackString:command.callbackId]]; 
    [*]} 
    [*]- (void)webViewDidFinishLoad:(UIWebView *)webView 
    [*]{ 
    [*] NSLog(@"Html2Pdf webViewDidFinishLoad"); 
    [*] UIPrintPageRenderer *render = [[UIPrintPageRenderer alloc] init]; 
    [*] [render addPrintFormatter:webView.viewPrintFormatter startingAtPageAtIndex:0]; 
    [*] CGRect printableRect = CGRectMake(self.pageMargins.left, 
    [*] self.pageMargins.top, 
    [*] self.pageSize.width - self.pageMargins.left - self.pageMargins.right, 
    [*] self.pageSize.height - self.pageMargins.top - self.pageMargins.bottom); 
    [*] CGRect paperRect = CGRectMake(0, 0, self.pageSize.width, self.pageSize.height); 
    [*] [render setValue:[NSValue valueWithCGRect:paperRect] forKey:@"paperRect"]; 
    [*] [render setValue:[NSValue valueWithCGRect:printableRect] forKey:@"printableRect"]; 
    [*] if (filePath) { 
    [*] [[render printToPDF] writeToFile: filePath atomically: YES]; 
    [*] } 
    [*] / 
    [*] [webView stopLoading]; 
    [*] webView.delegate = nil; 
    [*] [webView removeFromSuperview]; 
    [*] webView = nil; 
    [*] / 
    [*] [self success]; 
    [*] / 
    [*] NSURL* url = [NSURL fileURLWithPath:filePath]; 
    [*] self.documentController = [UIDocumentInteractionController interactionControllerWithURL:url]; 
    [*] documentController.delegate = self; 
    [*] UIView* view = self.webView.superview; 
    [*] CGRect rect = view.frame; / 
    [*] rect.size.height *= 0.02; 
    [*] BOOL isValid = [documentController presentOpenInMenuFromRect:rect inView:view animated:YES]; 
    [*] if (!isValid) { 
    [*] NSString* messageString = [NSString stringWithFormat:@"No PDF reader was found on your device. Please download a PDF reader (eg. iBooks or Acrobat)."]; 
    [*] UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:messageString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [*] [alertView show]; 
    [*] / 
    [*] } 
    [*]} 
    [*]- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error 
    [*]{ 
    [*] NSLog(@"webViewDidFailLoadWithError"); 
    [*] / 
    [*] [self error:[error description]]; 
    [*]} 
    [*]@end
    [*]@implementation UIPrintPageRenderer (PDF) 
    [*]- (NSData*) printToPDF 
    [*]{ 
    [*] NSMutableData *pdfData = [NSMutableData data]; 
    [*] UIGraphicsBeginPDFContextToData( pdfData, self.paperRect, nil ); 
    [*] [self prepareForDrawingPages: NSMakeRange(0, self.numberOfPages)]; 
    [*] CGRect bounds = UIGraphicsGetPDFContextBounds(); 
    [*] for ( int i = 0 ; i < self.numberOfPages ; i++ ) 
    [*] { 
    [*] UIGraphicsBeginPDFPage(); 
    [*] int aInteger = i; 
    [*] NSLog(@"page: %i", aInteger); 
    [*] [self drawPageAtIndex: i inRect: bounds]; 
    [*] } 
    [*] UIGraphicsEndPDFContext(); 
    [*] return pdfData; 
    [*]} 
    [*]@end
    
     
  2. maflynn Moderator

    maflynn

    Staff Member

    Joined:
    May 3, 2009
    Location:
    Boston

Share This Page