ios – 如何将PDF作为输入传递给打印机

ios – 如何将PDF作为输入传递给打印机,第1张

概述我是iPhone开发的新手,我需要打印UIView.所以我将UIView转换成PDF,它对我来说很好..  但我不知道如何将这个PDF传递打印机以便打印,任何人都可以帮助解决这个问题 提前致谢 我的代码是: - (void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename{ 我是iPhone开发的新手,我需要打印UIVIEw.所以我将UIVIEw转换成pdf,它对我来说很好..
但我不知道如何将这个pdf传递给打印机以便打印,任何人都可以帮助解决这个问题

提前致谢

我的代码是:

- (voID)createpdffromUIVIEw:(UIVIEw*)aVIEw savetodocumentsWithfilename:(Nsstring*)afilename{    NSMutableData *pdfData = [NSMutableData data];    UIGraphicsBeginPdfcontextToData(pdfData,aVIEw.bounds,nil);    UIGraphicsBeginpdfpage();    CGContextRef pdfcontext = UIGraphicsGetCurrentContext();    [aVIEw.layer renderInContext:pdfcontext];    UIGraphicsEndPdfcontext();       NSArray* documentDirectorIEs = NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,NSUserDomainMask,YES);    Nsstring* documentDirectory = [documentDirectorIEs objectAtIndex:0];    Nsstring* documentDirectoryfilename = [documentDirectory stringByAppendingPathComponent:afilename];    [pdfData writetofile:documentDirectoryfilename atomically:YES];    NSLog(@"documentDirectoryfilename: %@",documentDirectoryfilename);}-(voID)getpdf{     NSArray *paths = NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,YES);      Nsstring *documentsPath = [paths objectAtIndex:0];      Nsstring *filePath = [documentsPath stringByAppendingPathComponent:@"mypdf"];     NSLog(@"filePath: %@",filePath);     NSData *pngData = [NSData dataWithContentsOffile:filePath];    [displaypdfVIEw loadData:pngData MIMEType:@"application/pdf" textEnCodingname:@"utf-8" baseURL:nil];    [self.vIEw setBackgroundcolor:[UIcolor colorWithWhite:0.5 Alpha:0.0]];    displaypdfVIEw.hIDden = NO;}

//打印机代码

Nsstring *path = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"png"];    NSData *dataFromPath = [NSData dataWithContentsOffile:path];    UIPrintInteractionController *printController = [UIPrintInteractionController sharedPrintController];    if(printController && [UIPrintInteractionController canPrintData:dataFromPath]) {        printController.delegate = self;        uiprintinfo *printInfo = [uiprintinfo printInfo];        printInfo.outputType = uiprintinfoOutputGeneral;        printInfo.jobname = [path lastPathComponent];        printInfo.duplex = uiprintinfoDuplexLongEdge;        printController.printInfo = printInfo;        printController.showsPageRange = YES;        printController.printingItem = dataFromPath;        voID (^completionHandler)(UIPrintInteractionController *,BOol,NSError *) = ^(UIPrintInteractionController *printController,BOol completed,NSError *error) {            if (!completed && error) {                NSLog(@"Failed! due to error in domain %@ with error code %u",error.domain,error.code);            }        };        [printController presentFromrect:btnPrint.frame inVIEw:btnPrint.supervIEw                                animated:YES completionHandler:completionHandler];    }
解决方法 您可以通过此代码打印pdf ….
#if (READER_ENABLE_PRINT == TRUE) // OptionClass printInteractionController = NSClassFromString(@"UIPrintInteractionController");if ((printInteractionController != nil) && [printInteractionController isPrintingAvailable]){    NSURL *fileURL = document.fileURL; // document file URL    printInteraction = [printInteractionController sharedPrintController];    if ([printInteractionController canPrintURL:fileURL] == YES)    {        uiprintinfo *printInfo = [NSClassFromString(@"uiprintinfo") printInfo];        printInfo.duplex = uiprintinfoDuplexLongEdge;        printInfo.outputType = uiprintinfoOutputGeneral;        printInfo.jobname = document.filename;        printInteraction.printInfo = printInfo;        printInteraction.printingItem = fileURL;        printInteraction.showsPageRange = YES;        [printInteraction presentFromrect:button.bounds inVIEw:button animated:YES completionHandler:            ^(UIPrintInteractionController *pic,NSError *error)            {                #ifdef DEBUG                    if ((completed == NO) && (error != nil)) NSLog(@"%s %@",__FUNCTION__,error);                #endif            }        ];    }}  #endif //
总结

以上是内存溢出为你收集整理的ios – 如何将PDF作为输入传递给打印机全部内容,希望文章能够帮你解决ios – 如何将PDF作为输入传递给打印机所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1114323.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存