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

我是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) // Option

Class 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 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...