2011-05-06 97 views
1

此刻,我正在使用以下代碼進行打印。請注意,我有HTML打印的要求。在iOS中打印HTML

UIPrintInfo *printInfo = [UIPrintInfo printInfo]; 
printInfo.outputType = UIPrintInfoOutputGeneral; 
printInfo.jobName = @"Sample"; 
pic.printInfo = printInfo; 

NSString *htmlString = [self prepareHTMLEmailMessage]; 
UIMarkupTextPrintFormatter *htmlFormatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:htmlString]; 
htmlFormatter.startPage = 0; 
htmlFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); // 1-inch margins on all sides 
htmlFormatter.maximumContentWidth = 6 * 72.0; // printed content should be 6-inches wide within those margins 
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]; 
} 

產生的結果並不能爲所有頁面提供一致的邊距。部分問題是contentInsets屬性。根據Apple的文檔:頂部插圖僅適用於打印內容的第一頁。 Sample Output generated by the code above

我希望輸出具有一致的邊距,就像Safari產生的輸出一樣。 Print output produced by Safari

對此有何建議?

回答

0

使用UIPrintPageRenderer子類與UIMarkupTextPrintFormatter解決了我的問題。 Apple的示例代碼有所幫助。

2

聽起來你已經得到了你的工作的答案,但UIPrintPageRenderer不是絕對必要的,如果你正在做的是添加一個空的頁眉和頁腳墊出每個打印頁面 - 只是實例化一個香草UIPrintPageRenderer和設置headerHeightfooterHeight屬性,然後將UIMarkupTextPrintFormatter添加到渲染器。

我在您的其他問題上發佈了我的方法:Print paper size and content inset