pdf - Printing from an iOS device (AirPrint) -
i think must coming @ wrong angle. first foray pdf quartz. have ability print text entered app, may need drop in release.
i have read can hands on both apple , web, can't seem stuff working. can document printer prints blank. process using is:
- get text nsstring.
- convert string pdf in nsdata object.
- print it.
i have 2 methods in text project. test there no bells , whistles , there memory leaks, i'm trying work.
thanks in advance help.
the first creates pdf document nsdata.
- (nsdata *)createpdfasdatawithattributedstring:(nsattributedstring *)text { // save text in case… _pdftext = [text copy]; // allocate pdf context. cgcontextref pdfcontext; // create pdf attribute dictionary. cfmutabledictionaryref pdfattributedictionary = null; pdfattributedictionary = cfdictionarycreatemutable(null, 0, &kcftypedictionarykeycallbacks, &kcftypedictionaryvaluecallbacks); cfdictionarysetvalue(pdfattributedictionary, kcgpdfcontexttitle, cfstr("my note")); cfdictionarysetvalue(pdfattributedictionary, kcgpdfcontextcreator, cfstr("me")); // create data referece mutable data type. nsdata *pdfdata = [[nsmutabledata alloc] init]; // user data consumer using data reference. cgdataconsumerref pdfdataconsumer = cgdataconsumercreatewithcfdata((cfmutabledataref)pdfdata); // pdfcontext can created. pdfcontext = cgpdfcontextcreate(pdfdataconsumer, null, pdfattributedictionary); // start first page. cgcontextbeginpage(pdfcontext, null); // set font cgcontextselectfont(pdfcontext, "helvetica", 16.0f, kcgencodingmacroman); cgcontextsettextdrawingmode(pdfcontext, kcgtextfill); cgcontextsetrgbfillcolor(pdfcontext, 0, 0, 0, 1); // print text nsstring *regtext = [text string]; const char *pdftext = [regtext cstringusingencoding:nsutf8stringencoding]; cgcontextshowtext(pdfcontext, pdftext, strlen(pdftext)); // end page. cgcontextendpage(pdfcontext); // save current state cgcontextsavegstate(pdfcontext); // release pdf context cgcontextrelease(pdfcontext); return [pdfdata autorelease]; } the second print.
- (ibaction)printpdftouchupinside:(id)sender { pdfcreator *pdfcreator = [[pdfcreator alloc] init]; nsdata *pdfdata = [pdfcreator createpdfasdatawithattributedstring:_printabletext]; uiprintinteractioncontroller *printcontroller = [uiprintinteractioncontroller sharedprintcontroller]; uiprintinfo *printinfo = [uiprintinfo printinfo]; [printinfo setprinterid:@"canon mp620 series"]; [printinfo setorientation:uiprintinfoorientationportrait]; [printinfo setoutputtype:uiprintinfooutputgeneral]; [printinfo setjobname:@"test print"]; [printcontroller setprintinfo:printinfo]; [printcontroller setprintingitem:pdfdata]; void (^completionhandler)(uiprintinteractioncontroller *, bool, nserror *) = ^(uiprintinteractioncontroller *printcontroller, bool completed, nserror *error) { if (!completed && error) { nslog(@"failed! due error in domain %@ error code %u", error.domain, error.code); } }; [printcontroller presentfromrect:[sender bounds] inview:sender animated:yes completionhandler:completionhandler]; }
Comments
Post a Comment