cesarvr / pdf-generator

Cordova plugin to generate pdf in the client-side
MIT License
106 stars 62 forks source link

Impossible to include image in PDF from HTML after updating web-view on iOS #109

Closed askulikov closed 4 years ago

askulikov commented 5 years ago

After updating cordova-plugin-ionic-webview to 4.1.1 it is impossible to include images in pdf. All paths is not resolved. I spend about 3 days, trying find right path, but unsuccessfully. Diving deeper in native source of plugin, i found that base url will be empty string: PDFGenerator.m:137

if (data != NULL){
  NSURL *base =  [[NSURL alloc] initWithString:[[self.webViewEngine.URL absoluteString] stringByDeletingLastPathComponent]];
  // self.webViewEngine.URL === 'ionic://localhost'
  self.htmlPdfKit = [BNHtmlPdfKit saveHTMLAsPdf:data
                                  pageSize:pageSize
                                  isLandscape:landscape
                          baseUrl:base
                                  success:[self GetPDFHandler:command setOptions:option]
                                  failure:[self GetErrorHandler:command]];

  NSLog(@"url--> %@", [[base absoluteString] stringByDeletingLastPathComponent]); // output is 'url--> '
}

At this time output of self.webViewEngine.URL will be ionic://localhost. Unfortunately i'm not familiar in native code, but diving deeper in code, i found this in BNHtmlPdfKit.m:371 :

- (void)saveHtmlAsPdf:(NSString *)html toFile:(NSString *)file {
    self.outputFile = file;

    self.webView = [[WKWebView alloc] init];
    self.webView.navigationDelegate = self;

    if (!self.baseUrl) {
        [self.webView loadHTMLString:html baseURL:[NSURL URLWithString:@"http://localhost"]];
    } else {
        [self.webView loadHTMLString:html baseURL:self.baseUrl];
    }
}

As i understand, !self.baseUrl === true, and it pass first if-case, where for baseUrl will be assigned http://localhost. And i think it may crash all process.

Anyway, i'm not sure about my previous assumptions, but putting in PDFGenerator.m:143 baseUrl:[[NSBundle mainBundle] bundleURL] instead of baseUrl:base solve this problem.

Hope, it will save some time for somebody.

lucashsilva commented 4 years ago

I confirm this works. It would be nice to get an update!