Comments (18)
As far as I understand there are two methods in android to load html into webview
loadData and loadDataWithBaseURL
loadDataWithBaseURL permits to load local images like
file:///data/user/0/your.app.package.id/cache/abc.jpg
While loadData does not permit to load local files.
In my app i needed to create PDFs with local images
so in java/android/print/pdfConverter.java I changed:
//mWebView.loadData(mHtmlString, "text/HTML; charset=utf-8", null);
mWebView.loadDataWithBaseURL("", mHtmlString, "text/HTML", "utf-8", null);
and it worked for me
Interestingly, ability to load html with loadDataWithBaseURL was added to the project in commit 7028f6e
but for some reason was removed in commit e33b86f
from react-native-html-to-pdf.
This work for me on both Android and iOS:
- base64 images
- Local image files
- Images packaged in Android assets or iOS Bundle:
- base64 embedded image data
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAOwgAADsIBFShKgAAAABh0RVh0U29mdHdhcmUAcGFpbnQubmV0IDQuMC41ZYUyZQAAA5NJREFUWEfVl7tOG0EUhrdJHiB5gETJE3C1wUiAbUDiJiouBS096aBBlEDtmA4QJWkBwSsgUVJQoJACgQIWEtiyYsxkvuXMenbXXq9JiuSXjmTPmfn/45kzZ46d/worKytv+/r6UgMDA1+6uro20un0t8HBwT0+M6YtxRyZ/vegRT739PTktVCho6NDRVl3d3eht7c3pwP9JMtfj/n5+fepVOqrJq4GhWJYlbVwCF1rmJiYSCYSiR9BYr0LanZ2Vi0tLXljy8vL7hg+ey4GB1xCGw+ZTCaryco20cjIiNre3la3t7cKVKtVNT4+7trz87M7hm9ra0sNDw/7gtBcpWw2mxH6aBCtLd7Z2anW19dVqVRyRWwUi0XXgmBsbW3NXWsHMTk5mRCZ+uC87G3Xn9Xh4aHQhrG/v68ODg7kWxishcPiu1xcXHwncmFIwrmTiT5KHExPT6u5uTn5Vh9wGE4MDZHzg6umJ3jZzrZHgXMfHR1VU1NTMtIYcBne9vb2is6njyJbA/fcTCLh6p25DQLQhced2wyPj49qaGjICwItkX0B1UsniVdkyPZmqFQqELlBmFsQBTgNvy5WPxcWFt6IvONQXo1TB+JdtSiUy2W2UyWTSfdKNsPNzY3vVvT399dqA3XcOCgocVAoFFRbW5tr7EYczMzMeAGgKfKOo3/1hnFQ4Z6enmRJY+RyOY8sn8/LaGOwS3b1RFPkHYdXzTgwqlu9AmNwd3fnK7t8vr+/F28YDw8PamxszJuPoSny7hXcs526GkYGwPbbAWBUvkaoFwCaIu8/Ah6WOFm9ubkZCmJ3d1e8YcAJt5mLpsi/LgkB276zs+MFQpYfHx+LNwwqp9HxJaH+0vI1tHF0dOReR9ZTG05PT8VTw/X1tSeO+a4hhYhOxjh5UlvFyckJpO56fb7q4uJCPC+ILESANspM4D2PSsJGOD8/d0szHJy3QbAUoyWyNdDDaaf3GEVldRSurq7U6uqqOjs7kxHlchleXT1/6R36ILJ+tPocx0Hs5xi02pA0A2sDDcn3yIYESEtWMosw3nPOMQgKDBYEc+1tx3TiFXUx6haZaNBABoMgichkXjXAe0FDYjelXDXm2AmHIa7PPS308UADqXFpE2HkBq9asC23i4xtbHvsXx4E50XS0EbVI48ysp21Tc88DujhaKMoIPXEbGMO97zhVfsTUL0oodRxHpPgn1N8oQr3b8NxfgOgvsOjwyVIMgAAAABJRU5ErkJggg==" />
- Local image file:
Android:
<img
src="file:///storage/emulated/0/documents/acme-electronics.png"
style="height:26px;float:left;border:0px solid red"
/>
- Images packaged in Android assets or iOS Bundle:
Android assets:
<img
src="file:///android_asset/res/images/clientLogo.png"
style="height:26px;float:left;border:0px solid red" />
iOS resource bundle:
<img
src="/Users/edward3/Library/Developer/CoreSimulator/Devices
/F1680929-8154-4FB1-8210-E7F284F6A374/data/Containers/Bundle/Application
/920B65D1-16DC-4241-9DD9-685219DD43F9/FieldScout.app//res/images/clientLogo.png"
/>
from react-native-html-to-pdf.
This work for me on both Android and iOS:
- base64 images
- Local image files
- Images packaged in Android assets or iOS Bundle:
- base64 embedded image data
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAA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GEVldRSurq7U6uqqOjs7kxHlchleXT1/6R36ILJ+tPocx0Hs5xi02pA0A2sDDcn3yIYESEtWMosw3nPOMQgKDBYEc+1tx3TiFXUx6haZaNBABoMgichkXjXAe0FDYjelXDXm2AmHIa7PPS308UADqXFpE2HkBq9asC23i4xtbHvsXx4E50XS0EbVI48ysp21Tc88DujhaKMoIPXEbGMO97zhVfsTUL0oodRxHpPgn1N8oQr3b8NxfgOgvsOjwyVIMgAAAABJRU5ErkJggg==" />
- Local image file:
Android: <img src="file:///storage/emulated/0/documents/acme-electronics.png" style="height:26px;float:left;border:0px solid red" />
- Images packaged in Android assets or iOS Bundle:
Android assets:<img src="file:///android_asset/res/images/clientLogo.png" style="height:26px;float:left;border:0px solid red" />
iOS resource bundle:
<img src="/Users/edward3/Library/Developer/CoreSimulator/Devices /F1680929-8154-4FB1-8210-E7F284F6A374/data/Containers/Bundle/Application /920B65D1-16DC-4241-9DD9-685219DD43F9/FieldScout.app//res/images/clientLogo.png" />
https://github.com/christopherdro/react-native-html-to-pdf/issues/92#issuecomment-816059653
#92
i had the same issue, these comments helped me a lot.
this is how it worked for me to get the assets image
<img src="file:\\android_asset\\images\\img.png" />
from react-native-html-to-pdf.
Similar problem here (using Android emulator): images from the web (https) are working fine, but local files inside the document folder do not, for example by adding <img src="file:///storage/emulated/0/Documents/test.png"/>
at the end of the HTML string in the example app (and having this image at the location). Also tried without file://, same result: I get the PDF but without any image content.
from react-native-html-to-pdf.
Sorry for the question, but how should i use that baseURL, cause everything is working fine on iOS, but in Android the images are empty.
Thank you in advance
from react-native-html-to-pdf.
@gerriet Perhaps an Android manifest permissions issue?
WRITE_EXTERNAL_STORAGE gives your READ and WRITE
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
from react-native-html-to-pdf.
Same issue as @gerriet in iOS simulator too
from react-native-html-to-pdf.
@esutton File access seems not to be the problem as I can read the file into a base64 string (via react-native-fs) and with this string I get the image into the PDF.
from react-native-html-to-pdf.
@gerriet Sorry, I am not sure why it will not work for you.
I like minimal examples, however, it would be nice to have an example that contains an image file and adds path to html image src, and creates PDF, and displays it. I may do this if I find spare time.
For the app I am developing, I added a view and used react-native-pdf to help quickly display the generated PDF to help debug efficiently.
from react-native-html-to-pdf.
I have exactly the same problem.
<img src="file:///storage/emulated/0/Documents/test.png"/>
do not show the image in PDF
(where '/storage/emulated/0/Documents/test.png' is valid path)
With base64 work, but I do not want to keep the base64 data in my redux store for each photo because it make my app too slow
from react-native-html-to-pdf.
i have the same issue , i'm using firebase url for my images but it shows no images
from react-native-html-to-pdf.
@SychevSP Your fix worked for me for android, but it feels dirty modifying it directly. Do we know if it was removed on accident?
from react-native-html-to-pdf.
i have this same issue, works only image from web in Android. local files do not. Any suggestions?
thanks a lot
from react-native-html-to-pdf.
As far as I understand there are two methods in android to load html into webview
loadData and loadDataWithBaseURLloadDataWithBaseURL permits to load local images like
file:///data/user/0/your.app.package.id/cache/abc.jpg
While loadData does not permit to load local files.In my app i needed to create PDFs with local images
so in java/android/print/pdfConverter.java I changed://mWebView.loadData(mHtmlString, "text/HTML; charset=utf-8", null); mWebView.loadDataWithBaseURL("", mHtmlString, "text/HTML", "utf-8", null);and it worked for me
Interestingly, ability to load html with loadDataWithBaseURL was added to the project in commit 7028f6e
but for some reason was removed in commit e33b86f
It worked for me in Android. Thanks a lot!
from react-native-html-to-pdf.
@fedoud32 I was facing the same issue, firebase storage image urls not showing in PDF. I had to convert them into base64 string and pass it in html img src like this:
<img src=${`data:image/png;base64,${user.img_base_64}`} />
where user.img_base_64
contains base64 string of image.
from react-native-html-to-pdf.
If anyone wants, they can do yarn remove react-native-html-to-pdf
and then yarn add https://github.com/ConvergeTechnology/react-native-html-to-pdf.git
to install the repo where I made the change that @SychevSP suggested.
This fix works for me on android, and after you add the new package, all the usage and everything is the same.
from react-native-html-to-pdf.
As far as I understand there are two methods in android to load html into webview
loadData and loadDataWithBaseURLloadDataWithBaseURL permits to load local images like
file:///data/user/0/your.app.package.id/cache/abc.jpg
While loadData does not permit to load local files.In my app i needed to create PDFs with local images
so in java/android/print/pdfConverter.java I changed://mWebView.loadData(mHtmlString, "text/HTML; charset=utf-8", null); mWebView.loadDataWithBaseURL("", mHtmlString, "text/HTML", "utf-8", null);and it worked for me
Interestingly, ability to load html with loadDataWithBaseURL was added to the project in commit 7028f6e
but for some reason was removed in commit e33b86f
Nice man, you just saved me days of working. Thanks a million 👏👏👏👏
from react-native-html-to-pdf.
The fix is released in v0.9.0
from react-native-html-to-pdf.
Related Issues (20)
- How to Remove padding in android ? HOT 1
- in html content conditional statement are not wokring HOT 2
- PDFs of identical contents have different checksum every run HOT 1
- How can I add a footer & header to each page? HOT 3
- can i use this package on react native windows? HOT 2
- When there is a qrcode in Html it not shows in pdf - android HOT 1
- Crash in iOS HOT 1
- Unable to display pageNumber in pdf for Ios HOT 1
- is there a way to generate pdf without page break?
- Item's background colors is not showing in ios HOT 8
- How to add <img> html tag with local image in RN project folder? HOT 2
- This library is not working in the android SDK 33 HOT 3
- [TypeError: Cannot read property 'convert' of null] HOT 3
- Creating an extra page in the pdf in iOS, when creating page by loops
- Error when building the app on android
- CSS `background-color` and `background` property doesn't work on iOS HOT 5
- generate pdf on a single page HOT 1
- React native html to pdf with <embed><embed/> with ios 17.4
- Cannot read property 'convert' of null : this occurs while using react native expo HOT 1
- padding support for android HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-html-to-pdf.