Reputation: 383
I'm trying to convert HTML to a PDF file on OS X. After lots of digging, I found that the best way to do it is by using an NSPrintOperation
. This is the code I'm using to convert an HTML string to a PDF:
WebView *webview = [[WebView alloc] init];
[webview.mainFrame loadHTMLString:htmlString baseURL:nil];
NSDictionary *printOpts = @{
NSPrintJobDisposition: NSPrintSaveJob,
NSPrintSavePath: outputFilePath
};
NSPrintInfo *printInfo = [[NSPrintInfo alloc] initWithDictionary:printOpts];
printInfo.horizontalPagination = NSAutoPagination;
printInfo.verticalPagination = NSAutoPagination;
NSPrintOperation *printOp = [NSPrintOperation
printOperationWithView:webview.mainFrame.frameView.documentView
printInfo:printInfo];
printOp.showsPrintPanel = NO;
printOp.showsProgressPanel = NO;
[printOp runOperation];
This code does produce a PDF file, but the PDF file is empty. Is it because the WebView
doesn't have a frame? I've tried using dataWithPDFInsideRect:
, but that doesn't work either.
Should I initialise the webview with a frame the size of an A4 sheet? Or is the problem somewhere else?
Upvotes: 2
Views: 846
Reputation: 146
I know this is an old thread but I wanted to post the solution I came up with today. Since WebViews load asynchronously, the key is to set a frame load delegate callback then run through the run loop until the frame is finished loading.
My code below creates a fresh WebView then loads an HTML file into it:
// Clear the loaded flag
_webViewIsLoaded = NO;
// Fetch the default paper size and init print settings
NSPrintInfo * sharedInfo = [NSPrintInfo sharedPrintInfo];
NSMutableDictionary * sharedDict = [sharedInfo dictionary];
NSMutableDictionary * printInfoDict = [NSMutableDictionary dictionaryWithDictionary:sharedDict];
NSRect printRect = NSZeroRect;
printRect.size = [sharedInfo paperSize];
// Create a new web view
WebView * printView = [[WebView alloc] initWithFrame:printRect];
[printView setFrameLoadDelegate:self];
// Configure the WebView
WebPreferences * preferences = [[WebPreferences alloc] initWithIdentifier:@"com.id.here"];
[preferences setShouldPrintBackgrounds:YES];
[preferences setAllowsAnimatedImageLooping:NO];
[preferences setJavaScriptCanOpenWindowsAutomatically:NO];
[preferences setAutosaves:NO];
[preferences setJavaEnabled:NO];
[preferences setJavaScriptEnabled:NO];
[preferences setCacheModel:WebCacheModelDocumentViewer]; // most memory-efficient setting
[preferences setPlugInsEnabled:NO]; // disable plugins
[printView setPreferences:preferences];
// Load the HTML file
NSURL * url = [NSURL fileURLWithPath:htmlPath];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
[[printView mainFrame] loadRequest:request];
[printInfoDict setObject:NSPrintSaveJob
forKey:NSPrintJobDisposition];
[printInfoDict setObject:saveUrl forKey:NSPrintJobSavingURL];
NSPrintInfo * printInfo = [[NSPrintInfo alloc] initWithDictionary:printInfoDict];
[printInfo setHorizontalPagination:NSFitPagination];
[printInfo setVerticalPagination:NSAutoPagination];
[printInfo setVerticallyCentered:NO];
[printInfo setHorizontallyCentered:YES];
// Wait for the frame to finish loading
while(!_webViewIsLoaded)
{
@autoreleasepool
{
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.005]];
}
}
NSPrintOperation * printOp = [NSPrintOperation printOperationWithView:[[[printView mainFrame] frameView] documentView]
printInfo:printInfo];
[printOp setShowsProgressPanel:YES];
[printOp setShowsPrintPanel:NO];
[printOp runOperation];
Next, my frame load delegate method sets a flag once the frame is fully loaded and also resizes the height of the frame to match that of the contents:
-(void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)webFrame
{
_webViewIsLoaded = YES;
NSRect webFrameRect = [[[webFrame frameView] documentView] frame];
NSRect webViewRect = [webView frame];
// Resize the webview so it fits all of its contents
NSRect newWebViewRect = NSMakeRect(webViewRect.origin.x,
webViewRect.origin.y - (NSHeight(webFrameRect) - NSHeight(webViewRect)),
NSWidth(webViewRect),
NSHeight(webFrameRect));
[webView setFrame:newWebViewRect];
}
This should work well for saving HTML documents to PDF.
Upvotes: 3
Reputation: 19
Try this:
NSPrintOperation *printOp = [NSPrintOperation
printOperationWithView:[[[webview mainFrame] frameView] documentView] printInfo:printInfo];
Instead of:
NSPrintOperation *printOp = [NSPrintOperation printOperationWithView:webview.mainFrame.frameView.documentView
printInfo:printInfo];
This should work :)
EDIT: This is the full code that I used
NSString *htmlString = @"<html><head></head><body><h1>Hello, World</h1></body></html>";
WebView *webview = [[WebView alloc] init];
[webview.mainFrame loadHTMLString:htmlString baseURL:nil];
NSString* outputFilePath = @"/Users/YOUR-USERNAME/Documents/MyAwesomePDF.pdf";
NSURL *url = [[NSURL alloc] initWithString:outputFilePath];
NSDictionary *printOpts = @{
NSPrintJobDisposition: NSPrintSaveJob,
NSPrintSaveJob: url
};
NSPrintInfo *printInfo = [[NSPrintInfo alloc] initWithDictionary:printOpts];
printInfo.horizontalPagination = NSAutoPagination;
printInfo.verticalPagination = NSAutoPagination;
NSPrintOperation *printOp = [NSPrintOperation
printOperationWithView:[[[webview mainFrame] frameView] documentView] printInfo:printInfo];
printOp.showsPrintPanel = NO;
printOp.showsProgressPanel = NO;
[printOp runOperation];
Upvotes: 0