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

Images missing after change to WKWebView #47

Open
HenningH-DEV opened this issue Jul 5, 2021 · 3 comments
Open

Images missing after change to WKWebView #47

HenningH-DEV opened this issue Jul 5, 2021 · 3 comments

Comments

@HenningH-DEV
Copy link

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
Copy link

GManShen commented Dec 3, 2021

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

@sorinmiroiu97
Copy link

sorinmiroiu97 commented Aug 11, 2022

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
Copy link

chkpnt commented Aug 21, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants