5

我正在使用以下代码打印包含文本和图像的 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)打印的结果是,在某些情况下,字体样式会丢失。结果,内容被向下移动。为什么?

截屏

4

1 回答 1

9
  1. 具体选择A4纸张大小,我实现了协议的printInteractionController:choosePaper:方法,<UIPrintInteractionControllerDelegate>如果打印机支持,返回一个A4纸张大小(用 测试[UIPrintPaper bestPaperForPageSize:withPapersFromArray:]。注意这里没有设置纵向/横向,而是通过UIPrintInfo属性orientation.

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

    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. 无法帮助这个对不起。

于 2011-05-13T06:57:36.653 回答