iclems / iOS-htmltopdf

MIT License
305 stars 79 forks source link

Images missing after change to WKWebView #47

Open HenningH-DEV opened 3 years ago

HenningH-DEV commented 3 years ago

Hello,

I suddenly changed from an old version of NDHTMLtoPDF with UIWebView to the current version with WKWebView. Then problem now is, that I'm missing the images in the pdf. Is this a bug or do I need to change something in the code?

Code for the image (the logo)

NSString *fileURLString = [[[NSBundle mainBundle] URLForResource:@"Logo" withExtension:@"png"] absoluteString];
[htmlStrForPDF appendString:[NSString stringWithFormat:@"<td style=\"border-style:hidden\"; align=\"right\"><img src=\"%@\"/ alt=\"Logo\"; style=\"width:74px; height:50px\"></td>", fileURLString]];
[htmlStrForPDF appendString:@"</tr></table>"];

Code for creating the pdf

PlanExport *export = [[PlanExport alloc] init];                                                   
NSMutableString *htmlStrForPDF = [export createPDFPlan];
CGSize paperSize = CGSizeMake(839, 595); // DIN A4

NSString *ns_pathForFile = [NSString stringWithFormat:@"%@.%@",LocalizedString(@"PlanFileName", nil), LocalizedString(@"ExportFormatPDF", nil)];

self.PDFCreator = [NDHTMLtoPDF createPDFWithHTML:htmlStrForPDF pathForPDF:[AppCommon GetPathForFile:ns_pathForFile] pageSize:paperSize margins:UIEdgeInsetsMake(10, 15, 20, 15) successBlock:^(NDHTMLtoPDF *htmlToPDF) {
   NSString *result = [NSString stringWithFormat:@"HTMLtoPDF did succeed (%@ / %@)", htmlToPDF, htmlToPDF.PDFpath];
   NSLog(@"%@",result);

   // Send the generated file through Email.
   [self showEmail];

} errorBlock:^(NDHTMLtoPDF *htmlToPDF) {
   NSString *result = [NSString stringWithFormat:@"HTMLtoPDF did fail (%@)", htmlToPDF];
   NSLog(@"%@",result);
}];
GManShen commented 2 years ago

i am also facing this issue , any luck? This happens randomly for me

sorinmiroiu97 commented 2 years ago

From what i've found so far it seems some internal stuff got changed from UIWebView to WKWebView. There were some private apis at play that were facilitating the pdf export process with images in UIWebView, even though you might have been using UIMarkupTextPrintFormatter alongside UIGraphicsBeginPDFContextToData. Now by only changing the webview from UI... to WK... it seems those private apis got changed intentionally or got lost. Unfortunately I haven't found another way yet on how to generate a pdf by using UIMarkupTextPrintFormatter so you can generate it without previewing it first. Although I have found a solution that works, but implies rendering the html in a WKWebView first and then instead of using UIMarkupTextPrintFormatter you grab the webview's viewPrintFormatter() and by using this one you can generate the pdf with images just fine.

Here's a helpful link: https://stackoverflow.com/questions/49824797/generate-pdf-from-html-in-wkwebview-with-images

EDIT:

Using a UIMarkupTextPrintFormatter does actually work. I tested it myself and it actually works. For a full explanation and more info please check on this link the comment to this answer: "SWIFT 3: It took me a while to figure this out, but if you use the prefix "file://" in front of your URL, then it'll work. Like so:". The comment is like this: "Actually, you will also need to wait some time after calling UIMarkupTextPrintFormatter(). Rendering takes some time, so if you export to pdf right away, your images have not finished rendering and will not show in the pdf document. More on this here: https://stackoverflow.com/questions/40239515/uimarkuptextprintformatter-never-renders-base64-images - Quoc Anh Tran".

Safe travels, brave adventurers.

chkpnt commented 1 year ago

I had no issues with images until iOS 16.4 SDK: If the app is build with Xcode 14.2, the images are present on all devices, but since Xcode 14.3, the images are only present in the PDF when not running on a device with iOS 16.4 or later.

I just compared my NDHTMLtoPDF.m with the one in this repository. I've

[webview.configuration.preferences setValue:@"TRUE" forKey:@"allowFileAccessFromFileURLs"];

in the viewDidLoad method. It seems, that this is no longer sufficient.

EDIT: It is still working when using img-tags. It just doesn't work using the background-image property. It doesn't seem to be related to allowFileAccessFromFileURLs, as even if a data-url is used, it doesn't work with background-image. In this case, the WKWebView itself renders the image, but it's not in the exported PDF.