我正在嘗試解決如何執行此操作。使用內存中的UIWebView在PhoneGap中生成PDF
注意:我不是一個有經驗的Objective-C開發者(因此爲什麼我首先使用PhoneGap的)
這樣做的缺點:我的UIWebView(不,不是的PhoneGap一個呈現webapp,第二個UIWebView在內存中創建並且不可見)不會呈現到PDF中。我只是得到一個空白的PDF。我會發布我的一些想法和代碼,希望有人會知道我做錯了什麼。
我的起點是,已經有針對PhoneGap的打印插件在這裏:
https://github.com/phonegap/phonegap-plugins/tree/master/iPhone/PrintPlugin
此插件創建於即時,你通過JavaScript傳遞一些HTML給它一個UIWebView ,然後它調用一些打印控制器來執行打印。
所以我借用了一些想法。然後,我注意到了生成這個真棒博客文章PDF的
http://www.ioslearner.com/convert-html-uiwebview-pdf-iphone-ipad/
所以我試圖將兩者結合到我自己的PhoneGap插件採取了一些HTML(從我的Web應用程序),並生成PDF上the-飛。
部首:
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#ifdef PHONEGAP_FRAMEWORK
#import <PhoneGap/PGPlugin.h>
#else
#import "PGPlugin.h"
#endif
@interface ExportPlugin : PGPlugin <UIWebViewDelegate> {
NSString* exportHTML;
}
@property (nonatomic, copy) NSString* exportHTML;
//This gets called from my HTML5 app (Javascript):
- (void) exportPdf:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
@end
MAIN:
#import "ExportPlugin.h"
@interface ExportPlugin (Private)
-(void) doExport;
-(void) drawPdf;
@end
@implementation ExportPlugin
@synthesize exportHTML;
- (void) exportPdf:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options{
NSUInteger argc = [arguments count];
if (argc < 1) {
return;
}
self.exportHTML = [arguments objectAtIndex:0];
[self doExport];
}
int imageName = 0;
double webViewHeight = 0.0;
- (void) doExport{
//Set the base URL to be the www directory.
NSString *dbFilePath = [[NSBundle mainBundle] pathForResource:@"www" ofType:nil ];
NSURL *baseURL = [NSURL fileURLWithPath:dbFilePath];
//Load custom html into a webview
UIWebView *webViewExport = [[UIWebView alloc] init];
webViewExport.delegate = self;
//[webViewExport loadHTMLString:exportHTML baseURL:baseURL];
[webViewExport loadHTMLString:@"<html><body><h1>testing</h1></body></html>" baseURL:baseURL];
}
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webViewExport
{
webViewHeight = [[webViewExport stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] integerValue];
CGRect screenRect = webViewExport.frame;
//WHY DO I HAVE TO SET THE SIZE? OTHERWISE IT IS 0
screenRect.size.width = 768;
screenRect.size.height = 1024;
double currentWebViewHeight = webViewHeight;
while (currentWebViewHeight > 0)
{
imageName ++;
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
//[[UIColor blackColor] set];
//CGContextFillRect(ctx, screenRect);
[webViewExport.layer renderInContext:ctx];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pngPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png",imageName]];
if(currentWebViewHeight < 960)
{
CGRect lastImageRect = CGRectMake(0, 960 - currentWebViewHeight, webViewExport.frame.size.width, currentWebViewHeight);
CGImageRef imageRef = CGImageCreateWithImageInRect([newImage CGImage], lastImageRect);
newImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
}
[UIImagePNGRepresentation(newImage) writeToFile:pngPath atomically:YES];
[webViewExport stringByEvaluatingJavaScriptFromString:@"window.scrollBy(0,960);"];
currentWebViewHeight -= 960;
}
[self drawPdf];
}
- (void) drawPdf
{
CGSize pageSize = CGSizeMake(612, webViewHeight);
NSString *fileName = @"Demo.pdf";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);
// Mark the beginning of a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);
double currentHeight = 0.0;
for (int index = 1; index <= imageName ; index++)
{
NSString *pngPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png", index]];
UIImage *pngImage = [UIImage imageWithContentsOfFile:pngPath];
[pngImage drawInRect:CGRectMake(0, currentHeight, pageSize.width, pngImage.size.height)];
currentHeight += pngImage.size.height;
}
UIGraphicsEndPDFContext();
}
@end
的第一個跡象的東西是不正確的,是上面我必須設置UIWebView.frame大小:
screenRect.size.width = 768;
screenRect.size.height = 1024;
但爲什麼? PhoneGap PrintPlugin不必這樣做。如果我沒有設置它,大小爲0,然後我得到很多上下文錯誤。
然後接下來的問題是UIWebView沒有渲染任何東西。第一個問題的症狀可能是什麼?
我該如何解決這個問題並找出問題所在?
UPDATE
我敢肯定,它可能無法在一個UIWebView層呈現的圖像內容,除非一個UIWebView實際上是可見的。
我不確定PhoneGap PrintPlugin是如何工作的。它似乎呈現它的UIWebView相當好,它不可見。
我目前正在嘗試將實際的PhoneGap UIWebView渲染到PDF中(而不是我自己的UIWebView)。但這並不理想。
這意味着我不得不隱藏所有工具欄和諸如此類的東西,然後泛的UIWebView的周圍,所以我捕捉到視區之外的一切。這並不理想,因爲用戶會直觀地看到發生這種情況!
以上第1點似乎無法正常工作,因爲iPad在動態地擺弄佈局時更新屏幕太慢。在iPad上,如果你非常快速地做視覺事情(例如平移屏幕),iPad太慢而不會顯示它。你最終只看到最終狀態。所以當我截取屏幕截圖時,屏幕在視覺上並沒有平移(儘管DOM說它有)。 (希望是有道理的)。
唉,令人沮喪。
我實際上需要相反的 - 將UIWebView的內容呈現爲PDF – asgeo1 2012-02-15 19:52:19