iphone - Issues with converting HTML to PDF on iOS -


i using following code convert html pdf (as posted cwehrung @ various places)

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"];  nsdata *pdfdata = [render printtopdf];  [pdfdata writetofile: self.pdfpath  atomically: yes]; 

created category on uiprintpagerenderer support:

-(nsdata*) printtopdf {     [self donotrasterizesubviews:self.view];      nsmutabledata *pdfdata = [nsmutabledata data];      uigraphicsbeginpdfcontexttodata( pdfdata, self.paperrect, nil );      [self preparefordrawingpages: nsmakerange(0, self.numberofpages)];      cgrect bounds = uigraphicsgetpdfcontextbounds();      ( int = 0 ; < self.numberofpages ; i++ )     {         uigraphicsbeginpdfpage();          [self drawpageatindex: inrect: bounds];     }      uigraphicsendpdfcontext();      return pdfdata; } 

the issue have alignment between images , text.

i uploaded html file here, , it's easy see issue in converted pdf file uploaded here. (see 3rd book , 6th book)

any appreciated!

we can convert html pdf in way-

open html in uiwebview , code is-

-(void)display { webviewheight = [[self.printwebview stringbyevaluatingjavascriptfromstring:@"document.body.scrollheight;"] integervalue];  cgrect screenrect = self.printwebview.frame;  double currentwebviewheight = webviewheight;   nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *imagecachedirpath = [documentsdirectory stringbyappendingpathcomponent:@"pdfimages"]; if (![[nsfilemanager defaultmanager] fileexistsatpath: imagecachedirpath]) {     [[nsfilemanager defaultmanager] createdirectoryatpath:imagecachedirpath withintermediatedirectories:no attributes:nil error:null]; } else {     nserror *error;     [[nsfilemanager defaultmanager] removeitematpath: imagecachedirpath error: &error];     [[nsfilemanager defaultmanager] createdirectoryatpath:imagecachedirpath withintermediatedirectories:no attributes:nil error:null];  }  while (currentwebviewheight > 0) {     imagename ++;      uigraphicsbeginimagecontext(screenrect.size);     cgcontextref ctx = uigraphicsgetcurrentcontext();     [[uicolor blackcolor] set];     cgcontextfillrect(ctx, screenrect);     [self.printwebview.layer renderincontext:ctx];     uiimage *newimage = uigraphicsgetimagefromcurrentimagecontext();     uigraphicsendimagecontext();     nsstring *pngpath = [imagecachedirpath stringbyappendingpathcomponent:[nsstring stringwithformat:@"%d.png",imagename]];     if(currentwebviewheight < 440)     {         cgrect lastimagerect = cgrectmake(0,440-currentwebviewheight, self.printwebview.frame.size.width, currentwebviewheight);         cgimageref lastimageref = cgimagecreatewithimageinrect([newimage cgimage], lastimagerect);         newimage = [uiimage imagewithcgimage:lastimageref];         cgimagerelease(lastimageref);     }     [uiimagepngrepresentation(newimage) writetofile:pngpath atomically:yes];     [self.printwebview stringbyevaluatingjavascriptfromstring:@"window.scrollby(0,440);"];     currentwebviewheight -= 440; } [self drawpdf]; }  - (void) drawpdf { cgsize pagesize = cgsizemake(612, webviewheight); nsstring *filename = @"demo.pdf"; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *imagecachedirpath = [documentsdirectory stringbyappendingpathcomponent:@"pdfimages"];  nsstring *pdffilename = [imagecachedirpath stringbyappendingpathcomponent:filename]; nslog(@"file path:%@",pdffilename);  uigraphicsbeginpdfcontexttofile(pdffilename, cgrectzero, nil);  // mark beginning of new page. uigraphicsbeginpdfpagewithinfo(cgrectmake(0, 0, pagesize.width, pagesize.height), nil);  double currentheight = 0.0; (int index = 1; index <= imagename ; index++) {     nsstring *pngpath = [imagecachedirpath stringbyappendingpathcomponent:[nsstring stringwithformat:@"%d.png", index]];     uiimage *pngimage = [uiimage imagewithcontentsoffile:pngpath];      [pngimage drawinrect:cgrectmake(0, currentheight, pagesize.width, pngimage.size.height)];     currentheight += pngimage.size.height; } uigraphicsendpdfcontext(); } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -