微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

html – 打印纸张尺寸和内容插入

我正在使用以下代码来打印包含文本和图像的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); 
// 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];
}

请参阅附件中的结果(按比例缩小的版本可能不是很清楚,但希望你能得到图片).

这是我的问题:

>打印纸尺寸如何由AirPrint确定?如果我想专门为A4纸格式化和打印数据怎么办?
>使用上述代码并使用不同的模拟打印机(打印机模拟器)打印的结果是,在所有情况下,我在第一页的顶部获得1英寸的边距,但在连续页面上没有.为什么?
>使用上述代码并使用不同的模拟打印机(打印机模拟器)进行打印的结果是,在某些情况下,字体样式会丢失.结果,内容向下移动.为什么?

解决方法

>为了专门选择A4纸张尺寸,我实现了printInteractionController:selectPaper:< UIPrintInteractionControllerDelegate>的方法.如果打印机支持,则返回A4纸张大小(使用[UIPrintPaper bestPaperForPageSize:withPapersFromArray:]进行测试.请注意,此处未设置纵向/横向,而是通过uiprintinfo属性方向设置.
>属性htmlFormatter.contentInsets仅在页面渲染器跨页面拆分之前设置整个内容的插入.通过UIPrintPageRenderer添加空白页眉和页脚,然后在HTML打印格式化程序的左侧和右侧添加1cm边距,我能够设置每页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];

>对不起这个抱歉.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐