Open HenningH-DEV opened 3 years ago
i am also facing this issue , any luck? This happens randomly for me
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.
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.
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)
Code for creating the pdf