我正在研究使用票据打印机将票据打印到连续纸卷上的代码。收据类似于您在商店中用信用卡付款时得到的收据。
我正在使用UIMarkupTextPrintFormatter,如下面的代码所示。
但是由于某种原因,我在打印文本的中间和结尾都一直保持空白。(在打印最后一行之后,打印机将继续滚动几英寸的白纸!)
我检查了pageCount属性,它返回2,所以可能这是多余的空白来自哪里。
我还启用了页面范围以显示页面数(用于调试)。我不应该在控制器上获得页面范围。但是我的意思是我的内容超过2页。
您能否指出我的代码不足之处?将HTML内容打印到连续纸上的正确方法是什么?
我的代码可以在打印机模拟器上完美运行。我得到了我想要的确切输出。没有意外的空格。但是在与AirPrint兼容的真实收据打印机上却失败了!
任何建议都非常感谢。
UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
if(!controller){
DDLogInfo(@"Couldn't get shared UIPrintInteractionController!");
return;
}
UIPrintInteractionCompletionHandler completionHandler = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
if(!completed && error)
DDLogInfo(@"FAILED! due to error in domain %@ with error code %ld", error.domain, (long)error.code);
};
controller.showsPageRange = YES;
controller.showsPaperSelectionForLoadedPapers = YES;
// Since the we're using a formatter, explicitly set the other properties to nil
controller.printingItem = nil;
controller.printingItems = nil;
controller.printPageRenderer = nil;
UIMarkupTextPrintFormatter *formatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:[self htmlPrintContent]];
formatter.startPage = 0;
formatter.contentInsets = UIEdgeInsetsZero;
controller.printFormatter = formatter;
// Ask for a print job object and configure its settings to tailor the print request
UIPrintInfo *info = [UIPrintInfo printInfo];
// B&W or color, normal quality output for mixed text, graphics, and images
info.outputType = UIPrintInfoOutputGrayscale;
// Select the job named this in the printer queue to cancel our print request.
info.jobName = @"somejobname";
// Make sure we are printing in a portrait orientation.
info.orientation = UIPrintInfoOrientationPortrait;
// We don't need duplex printing so set it to none explicitly
info.duplex = UIPrintInfoDuplexNone;
// Instruct the printing concierge to use our custom print job settings.
controller.printInfo = info;
// Present the standard iOS Print Panel that allows you to pick the target Printer, number of pages, etc.
[controller presentFromRect:self.printButton.frame inView:self.view animated:YES completionHandler:completionHandler];
我最终切换到UISimpleTextPrintFormatter而不是UIMarkupTextPrintFormatter。
所有格式化均使用NSAttributedString完成
也已实施
- (CGFloat)printInteractionController:(UIPrintInteractionController *)printInteractionController cutLengthForPaper:(UIPrintPaper *)paper
为了确定我的文字所需的高度。
CGFloat printableWidth = paper.printableRect.size.width;
CGRect attributedStringBoundingRect = [attributedPrintContent boundingRectWithSize:CGSizeMake(printableWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
/* Calculate the margins of the roll */
CGFloat lengthOfMargins = paper.paperSize.height - paper.printableRect.size.height;
/* The cut length is the height of the text, plus margins, plus content insets */
CGFloat cutLength = attributedStringBoundingRect.size.height + lengthOfMargins + formatter.contentInsets.bottom + formatter.contentInsets.top;
希望这可以帮助...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句