2015-01-05 4 views
1

영수증 프린터를 사용하여 영수증을 연속 롤 용지에 인쇄하는 코드 작업 중입니다. 영수증은 상점에서 신용 카드로 지불 할 때 얻는 것과 비슷합니다.AirPrint를 사용하여 연속 롤 용지에 인쇄 할 때 원치 않는 여분의 공백이 있음

아래 코드에서 볼 수있는 것처럼 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]; 

답변

1

결국 UIMarkupTextPrintFormatter 대신 UISimpleTextPrintFormatter로 전환되었습니다. 모든 포맷

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; 

희망 ...

0

나는 또한 boundingRectWithSize:options:context:를 호출 할 때 인쇄 폭에서 여백을 빼기 위해 필요한 것으로 나타났다. 예 :

func printInteractionController(_ printInteractionController: UIPrintInteractionController, cutLengthFor paper: UIPrintPaper) -> CGFloat { 
    let size = CGSize(width: paper.printableRect.width - margin * 2, height: 0) 

    let boundingRect = attributedText.boundingRect(with: size, options: [ 
     .usesLineFragmentOrigin, 
     .usesFontLeading 
    ], context: nil) 

    return boundingRect.height + margin * 2 
} 
관련 문제