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

标签 ios objective-c iphone pdf printing

我是 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,NSUserDomainMask, 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, BOOL completed, NSError *error)
            {
                #ifdef DEBUG
                    if ((completed == NO) && (error != nil)) NSLog(@"%s %@", __FUNCTION__, error);
                #endif
            }
        ];
    }
}

  #endif //

关于ios - 如何将 PDF 作为输入传递给打印机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10104382/

相关文章:

javascript - 使用 iframe 嵌入 iPhone YouTube

javascript - 如何在 Web 浏览器和 XCode 中使用 JavaScript 运行 HTML 文件?

ios - 使用github操作时如何在faSTLane中选择xcode版本

ios - 如何处理背景图像的 iphone 屏幕尺寸/分辨率

c# - 在将 ARKit 与 Unity 结合使用时访问 iPhone 相机帧,转换 camera.videoparams.cvPixelBuffer

objective-c - 如何从 Objective-C 中的 MS 项目中提取信息?

ios - 为什么我能够访问 Core Data 中除图像属性之外的所有属性? (这是二进制数据?)

iphone - 这是从后台线程访问 UI 工具包的安全方法吗...?

objective-c - 字符串分离

ios - 多个 NSMutableArrays,一个按 Predicate 排序,其他的相对重新排序