[Trans]AirPrint in webview&transfer to PDF
12859 ワード
http://www.cnblogs.com/kiss007/archive/2012/06/26/2563662.html
実例1:
Bossが調べてみます。これをPDFに変換する方法は、googleがたくさんあります。webviewでロードし、スクリーンショットし、変換します。Bossに報告したところ、このように変換されたpdfは純画像で、テキストを操作できなくなり、希望の効果が得られないと言われました。しょうがない、続けてgoogle。結果として、もう一度、stackoverflowの強さを味わいました。最終的に解決策を見つけました。http://stackoverflow.com/q/9528658/966127実現過程、サブクラス化UICPrintPageRenderer
実例1:
Class printInteractionController = NSClassFromString(@"UIPrintInteractionController");
if ((printInteractionController != nil) && [printInteractionController isPrintingAvailable])
{
printInteraction = [printInteractionController sharedPrintController];
printInteraction.delegate = self;
UIPrintInfo *printInfo = [NSClassFromString(@"UIPrintInfo") printInfo];
printInfo.duplex = UIPrintInfoDuplexLongEdge;
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
printInteraction.printInfo = printInfo;
printInteraction.showsPageRange = YES;
UIViewPrintFormatter *formatter = [webView viewPrintFormatter];
printInteraction.printFormatter = formatter;
[printInteraction presentFromBarButtonItem:actionButtonItem
animated:YES
completionHandler:
^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
}
];
}
実例2: Class printInteractionController = NSClassFromString(@"UIPrintInteractionController");
if ((printInteractionController != nil) && [printInteractionController isPrintingAvailable])
{
NSURL *fileURL = document.fileURL; // Document file URL
printInteraction = [printInteractionController sharedPrintController];
if ([printInteractionController canPrintURL:fileURL] == YES) // Check first
{
UIPrintInfo *printInfo = [NSClassFromString(@"UIPrintInfo") printInfo];
printInfo.duplex = UIPrintInfoDuplexLongEdge;
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = document.fileName;
printInteraction.printInfo = printInfo;
printInteraction.printingItem = fileURL;
printInteraction.showsPageRange = YES;
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
[printInteraction presentFromRect:button.bounds inView:button animated:YES completionHandler:
^(UIPrintInteractionController *pic, BOOL completed, NSError *error)
{
#ifdef DEBUG
if ((completed == NO) && (error != nil)) NSLog(@"%s %@", __FUNCTION__, error);
#endif
}
];
}
else // Presume UIUserInterfaceIdiomPhone
{
[printInteraction presentAnimated:YES completionHandler:
^(UIPrintInteractionController *pic, BOOL completed, NSError *error)
{
#ifdef DEBUG
if ((completed == NO) && (error != nil)) NSLog(@"%s %@", __FUNCTION__, error);
#endif
}
];
}
}
}
UPPrintPageRendererを利用してhtmlをpdfに変換します。 Bossが調べてみます。これをPDFに変換する方法は、googleがたくさんあります。webviewでロードし、スクリーンショットし、変換します。Bossに報告したところ、このように変換されたpdfは純画像で、テキストを操作できなくなり、希望の効果が得られないと言われました。しょうがない、続けてgoogle。結果として、もう一度、stackoverflowの強さを味わいました。最終的に解決策を見つけました。http://stackoverflow.com/q/9528658/966127実現過程、サブクラス化UICPrintPageRenderer
- (CGRect) paperRect
{
if (!_generatingPdf)
return [super paperRect];
return UIGraphicsGetPDFContextBounds();
}
- (CGRect) printableRect
{
if (!_generatingPdf)
return [super printableRect];
return CGRectInset( self.paperRect, 20, 20 );
}
- (NSData*) printToPDF
{
_generatingPdf = YES;
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData( pdfData, CGRectMake(0, 0, 792, 612), nil ); // letter-size, landscape
[self prepareForDrawingPages: NSMakeRange(0, 1)];
CGRect bounds = UIGraphicsGetPDFContextBounds();
for ( int i = 0 ; i < self.numberOfPages ; i++ )
{
UIGraphicsBeginPDFPage();
[self drawPageAtIndex: i inRect: bounds];
}
UIGraphicsEndPDFContext();
_generatingPdf = NO;
// NSString* filename = @"/Volumes/Macintosh HD 2/test.pdf";
// [pdfData writeToFile: filename atomically: YES];
return pdfData;
}
UIViewPrintFormatter *viewFormatter = [htmlWebView viewPrintFormatter];
[ppRenderer addPrintFormatter:viewFormatter startingAtPageAtIndex:0];
NSData *pdfData = [ppRenderer printToPDF];
NSString *pdfPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/test.pdf"];
[pdfData writeToFile:pdfPath atomically:YES];