【问题标题】:How to print the pdf with the actual size in iOS如何在iOS中以实际大小打印pdf
【发布时间】:2013-09-03 03:12:04
【问题描述】:

这是一个令人沮丧的问题。

我正在使用UIPrintInteractionController 打印 PDF 文件。此 PDF 文件的大小为 A4, 595x842 in pt,但打印出来的内容是按比例缩小的。

根据我是否使用iPhone模拟器,结果甚至有所不同。当我在 iPhone 模拟器中测试以下代码时,我打印的文件是它的实际大小。但是当我在设备 (iPad) 中测试这些代码时,问题发生了,并带有日志“Deskjet\0323520\032series\032[5B73DB]._ipp._tcp.local.: Print-Job request successful with warning : 作业属性与打印文档不匹配。"

我写的代码是这样的:

- (IBAction)printButtonTouched:(UIButton *)sender {
    NSURL *testFileURL = [self makeTestPDF];
    if ([UIPrintInteractionController canPrintURL:testFileURL]) {
        UIPrintInfo *printInfo = [UIPrintInfo printInfo];
        printInfo.jobName = testFileURL.path;
        printInfo.orientation = UIPrintInfoOrientationPortrait;
        printInfo.outputType = UIPrintInfoOutputGeneral;
        printInfo.duplex = UIPrintInfoDuplexNone;

        self.myPrinter.printInfo =printInfo;
        self.myPrinter.printingItem = testFileURL;

        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            [self.myPrinter presentFromRect:sender.frame inView:self.view animated:YES completionHandler:^(UIPrintInteractionController *printInteractionController, BOOL completed, NSError *error) {

            }];
        } else {
            [self.myPrinter presentAnimated:YES completionHandler:^(UIPrintInteractionController *printInteractionController, BOOL completed, NSError *error) {

            }];
        }
    }
}

- (UIPrintPaper *)printInteractionController:(UIPrintInteractionController *)printInteractionController choosePaper:(NSArray *)paperList
{
    CGRect mediaBox = CGRectMake(0, 0, 595.276, 841.89);
    UIPrintPaper *bestPaper = [UIPrintPaper bestPaperForPageSize:mediaBox.size withPapersFromArray:paperList];
    return bestPaper;
}

当我在 PDF 上下文中开始一个 PDF 页面时,我的代码是这样的:

    CGFloat a4Width = 595.276f;
    CGFloat a4Height = 841.89f;
    CGRect mediaBox = CGRectMake(0, 0, a4Width, a4Height);
    UIGraphicsBeginPDFContextToFile(testPDFURL.path, mediaBox, nil);
    UIGraphicsBeginPDFPageWithInfo(mediaBox, nil);

我正在测试的打印机是 HP Deskjet Ink Advantage 3525
当我在另一台也支持 AirPrint 的打印机上进行测试时,同样的事情仍然发生,唯一的区别是缩小比例。

我正在寻找如何使 PDF 以实际大小打印的答案。而且我已经尝试了将近一周的时间来寻找答案,但我没有找到有用的信息。请帮帮我。

【问题讨论】:

  • 我也遇到了同样的问题。你能做到吗?

标签: ios objective-c pdf printing


【解决方案1】:

我正在做类似的事情,但就我而言,我打印了UIImage。我实现了一个UIPrintPageRender 子类并覆盖了drawContentForPage 方法。该方法公开了应该打印内容的CGRect

class ExtendedPageRenderer: UIPrintPageRenderer {
       override func drawContentForPage(at pageIndex: Int, in contentRect: CGRect) {
      // code that will draw/render in CGRect
    }
}

我使用这样的类:

let pageRenderer = ExtendedPageRenderer()
let printController = UIPrintInteractionController.shared
printController.printPageRenderer = pageRenderer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    • 1970-01-01
    • 2011-03-04
    • 2020-04-27
    • 1970-01-01
    • 2014-03-07
    相关资源
    最近更新 更多