Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated from UIWebView to WKWebView #147

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions ios/RNHTMLtoPDF/RNHTMLtoPDF.h
Expand Up @@ -3,7 +3,7 @@

#import <React/RCTView.h>
#import <React/RCTBridgeModule.h>

@interface RNHTMLtoPDF : RCTView <RCTBridgeModule, UIWebViewDelegate>
#import <WebKit/WebKit.h>
@interface RNHTMLtoPDF : RCTView <RCTBridgeModule, WKNavigationDelegate>

@end
41 changes: 20 additions & 21 deletions ios/RNHTMLtoPDF/RNHTMLtoPDF.m
Expand Up @@ -52,7 +52,7 @@ @implementation RNHTMLtoPDF {
UIColor *_bgColor;
NSInteger *_numberOfPages;
CGSize _PDFSize;
UIWebView *_webView;
WKWebView *_webView;
float _paddingBottom;
float _paddingTop;
float _paddingLeft;
Expand All @@ -73,8 +73,8 @@ + (BOOL)requiresMainQueueSetup
- (instancetype)init
{
if (self = [super init]) {
_webView = [[UIWebView alloc] initWithFrame:self.bounds];
_webView.delegate = self;
_webView = [[WKWebView alloc] initWithFrame:self.bounds];
_webView.navigationDelegate = self;
[self addSubview:_webView];
autoHeight = false;
}
Expand Down Expand Up @@ -173,44 +173,43 @@ - (instancetype)init

NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];

[_webView loadHTMLString:_html baseURL:baseURL];
dispatch_async(dispatch_get_main_queue(), ^{
[_webView loadHTMLString:_html baseURL:baseURL];
});

_resolveBlock = resolve;
_rejectBlock = reject;

}

- (void)webViewDidFinishLoad:(UIWebView *)awebView
{
if (awebView.isLoading)
return;

-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
if (webView.isLoading)
return;

UIPrintPageRenderer *render = [[UIPrintPageRenderer alloc] init];
[render addPrintFormatter:awebView.viewPrintFormatter startingAtPageAtIndex:0];

[render addPrintFormatter:webView.viewPrintFormatter startingAtPageAtIndex:0];
// Define the printableRect and paperRect
// If the printableRect defines the printable area of the page
CGRect paperRect = CGRectMake(0, 0, _PDFSize.width, _PDFSize.height);
CGRect printableRect = CGRectMake(_paddingTop, _paddingLeft, _PDFSize.width-(_paddingLeft + _paddingRight), _PDFSize.height-(_paddingBottom + _paddingTop));


[render setValue:[NSValue valueWithCGRect:paperRect] forKey:@"paperRect"];
[render setValue:[NSValue valueWithCGRect:printableRect] forKey:@"printableRect"];

NSData * pdfData = [render printToPDF:&_numberOfPages backgroundColor:_bgColor ];

if (pdfData) {
NSString *pdfBase64 = @"";

[pdfData writeToFile:_filePath atomically:YES];
if (_base64) {
pdfBase64 = [pdfData base64EncodedStringWithOptions:0];
}
NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
pdfBase64, @"base64",
[NSString stringWithFormat: @"%ld", (long)_numberOfPages], @"numberOfPages",
_filePath, @"filePath", nil];
pdfBase64, @"base64",
[NSString stringWithFormat: @"%ld", (long)_numberOfPages], @"numberOfPages",
_filePath, @"filePath", nil];
_resolveBlock(data);
} else {
NSError *error;
Expand Down