打印纸张尺寸和内容插图

发布于 2024-11-03 16:51:02 字数 2288 浏览 5 评论 0原文

我正在使用以下代码来打印包含文本和图像的 HTML 内容。

if (![UIPrintInteractionController isPrintingAvailable]) {
    UIAlertView *alertView = [[[UIAlertView alloc] 
        initWithTitle:NSLocalizedString(@"Printer Availability Error Title", @"")
        message:NSLocalizedString(@"Printer Availability Error Message", @"")
        delegate:nil
        cancelButtonTitle:NSLocalizedString(@"OK", @"OK")
        otherButtonTitles:nil] autorelease];
    [alertView show];
    return;
}

UIPrintInteractionController *pic = 
    [UIPrintInteractionController sharedPrintController];

if(!pic) {
    NSLog(@"Couldn't get shared UIPrintInteractionController!");
    return;
}

pic.delegate = self;

UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = @"Sample";
pic.printInfo = printInfo;

NSString *htmlString = [self prepareHTMLText];
UIMarkupTextPrintFormatter *htmlFormatter = 
    [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:htmlString];
htmlFormatter.startPage = 0;
// 1-inch margins on all sides
htmlFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); 
// printed content should be 6-inches wide within those margins
htmlFormatter.maximumContentWidth = 6 * 72.0;   
pic.printFormatter = htmlFormatter;
[htmlFormatter release];

pic.showsPageRange = YES;

void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {

    if (!completed && error) {
        NSLog(@"Printing could not complete because of error: %@", error);
    }
};

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    [pic presentFromBarButtonItem:self.myPrintBarButton 
        animated:YES 
        completionHandler:completionHandler];

} else {
    [pic presentAnimated:YES completionHandler:completionHandler];
}

结果请参阅附件(缩小版可能不是很清楚,但希望您能明白)。

我的问题如下:

  1. AirPrint 如何确定打印纸张尺寸?如果我想专门格式化并打印 A4 纸张的数据,该怎么办?

  2. 使用上述代码并使用不同的模拟打印机(打印机模拟器)进行打印的结果是,在所有情况下,我在第一页的顶部得到 1 英寸的边距,但在连续的页面上却没有。为什么?

  3. 使用上述代码并使用不同的模拟打印机(Printer Simulator)进行打印的结果是,在某些情况下,字体样式会丢失。结果,内容被下移。为什么?

屏幕截图

I'm using following code to print HTML content containing text and images.

if (![UIPrintInteractionController isPrintingAvailable]) {
    UIAlertView *alertView = [[[UIAlertView alloc] 
        initWithTitle:NSLocalizedString(@"Printer Availability Error Title", @"")
        message:NSLocalizedString(@"Printer Availability Error Message", @"")
        delegate:nil
        cancelButtonTitle:NSLocalizedString(@"OK", @"OK")
        otherButtonTitles:nil] autorelease];
    [alertView show];
    return;
}

UIPrintInteractionController *pic = 
    [UIPrintInteractionController sharedPrintController];

if(!pic) {
    NSLog(@"Couldn't get shared UIPrintInteractionController!");
    return;
}

pic.delegate = self;

UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = @"Sample";
pic.printInfo = printInfo;

NSString *htmlString = [self prepareHTMLText];
UIMarkupTextPrintFormatter *htmlFormatter = 
    [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:htmlString];
htmlFormatter.startPage = 0;
// 1-inch margins on all sides
htmlFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); 
// printed content should be 6-inches wide within those margins
htmlFormatter.maximumContentWidth = 6 * 72.0;   
pic.printFormatter = htmlFormatter;
[htmlFormatter release];

pic.showsPageRange = YES;

void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {

    if (!completed && error) {
        NSLog(@"Printing could not complete because of error: %@", error);
    }
};

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    [pic presentFromBarButtonItem:self.myPrintBarButton 
        animated:YES 
        completionHandler:completionHandler];

} else {
    [pic presentAnimated:YES completionHandler:completionHandler];
}

See attached for results (the scaled down version may not be very clear, but, hopefully, you get the picture).

Here are my questions:

  1. How is the print paper size determined by AirPrint? What if I want to specifically format and print data for A4 paper?

  2. The result of using the above code and printing using different simulated printers (Printer Simulator) is that, in all cases, I get a 1 inch margin on the top of first page, but not on consecutive pages. Why?

  3. The result of using the above code and printing using different simulated printers (Printer Simulator) is that, in some cases, the font style is lost. As a result, the content is shifted down. Why?

Screen-shot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

非要怀念 2024-11-10 16:51:02
  1. 为了专门选择 A4 纸张尺寸,我实现了 协议的 printInteractionController:choosePaper: 方法,并返回 A4 纸张尺寸(如果支持)打印机(使用 [UIPrintPaper bestPaperForPageSize:withPapersFromArray:] 进行测试。请注意,此处未设置纵向/横向方向,而是通过 UIPrintInfo 属性 orientation 设置.

  2. 属性htmlFormatter.contentInsets仅在页面渲染器将内容分割到多个页面之前设置整个内容的插图。我可以通过 UIPrintPageRenderer 添加空白页眉和页脚来设置 1 厘米的每页边距,然后在 HTML 打印格式化程序的左侧和右侧添加 1 厘米的边距:

    UIPrintPageRenderer *renderer = [[UIPrintPageRenderer alloc] init];
    渲染器.headerHeight = 30.0f;
    渲染器.footerHeight = 30.0f;
    pic.printPageRenderer = 渲染器;
    [渲染器发布];
    
    UIMarkupTextPrintFormatter *htmlFormatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText: htmlString];
    htmlFormatter.startPage = 0;
    htmlFormatter.contentInsets = UIEdgeInsetsMake(0.0f, 30.0f, 0.0f, 30.0f);
    [渲染器addPrintFormatter:htmlFormatterstartingAtPageAtIndex:0];
    [htmlFormatter 发布];
    
  3. Can'抱歉,我无法帮助解决此问题。

  1. To specifically select A4 paper size, I implemented the printInteractionController:choosePaper: method of the <UIPrintInteractionControllerDelegate> protocol, and returned an A4 paper size if supported by the printer (test with [UIPrintPaper bestPaperForPageSize:withPapersFromArray:]. Note that portrait/landscape orientation is not set here, but by the UIPrintInfo property orientation.

  2. The property htmlFormatter.contentInsets only sets the inset for the content as a whole, before it's been split across pages by the page renderer. I was able to set a 1cm per-page margin by adding blank headers and footers via a UIPrintPageRenderer, and then adding a 1cm margin to the left and right of the HTML print formatter:

    UIPrintPageRenderer *renderer = [[UIPrintPageRenderer alloc] init];
    renderer.headerHeight = 30.0f;
    renderer.footerHeight = 30.0f;
    pic.printPageRenderer = renderer;
    [renderer release];
    
    UIMarkupTextPrintFormatter *htmlFormatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText: htmlString];
    htmlFormatter.startPage = 0;
    htmlFormatter.contentInsets = UIEdgeInsetsMake(0.0f, 30.0f, 0.0f, 30.0f);
    [renderer addPrintFormatter: htmlFormatter startingAtPageAtIndex: 0];
    [htmlFormatter release];
    
  3. Can't help with this one sorry.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文