html - 打印纸张尺寸和内容插图

标签 html ios printing airprint uiprintinteractioncntrler

我正在使用以下代码打印包含文本和图像的 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. 使用上述代码并使用不同的模拟打印机 (Printer Simulator) 进行打印的结果是,在所有情况下,我在第一页的顶部都有 1 英寸的边距,但在连续的页面上没有。为什么?

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

Screen-shot

最佳答案

  1. 为了专门选择 A4 纸张尺寸,我实现了 printInteractionController:choosePaper: <UIPrintInteractionControllerDelegate> 的方法协议(protocol),如果打印机支持,则返回 A4 纸张尺寸(使用 [UIPrintPaper bestPaperForPageSize:withPapersFromArray:] 进行测试。请注意,此处未设置纵向/横向方向,而是由 UIPrintInfo 属性 orientation 设置。

  2. 属性(property) htmlFormatter.contentInsets仅在页面渲染器将内容拆分为多个页面之前,将内容作为一个整体设置插图。我可以通过 UIPrintPageRenderer 添加空白页眉和页脚来设置每页 1 厘米的页边距 | ,然后在 HTML 打印格式器的左右各增加 1cm 的边距:

    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. 抱歉,这件事帮不上忙。

关于html - 打印纸张尺寸和内容插图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5817840/

相关文章:

javascript - 这个 DIV 背景图像分配有什么问题?

javascript - 计算选中的复选框和文本区域的问题

javascript - 将选择列表值分配给数组

java - 用java打印pdf

html - 根据 URL 变量过滤 HTML 内容

ios - 在 .gitmodules 中找不到子模块路径 'Pods' 的 url

php + ios加密解密3des + base64

ios - UICollectionView:我需要使用 UICollectionViewController 吗?

c# - 通过 ESC/P 命令将位图写入 Epson TM88IV (Write To NVRam)

css - 打印 css 样式不适用于 Angular 元素