question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The PDF file is NOT Created - Wrong filePath

See original GitHub issue

Recently, RNHTMLtoPDF.convert() stopped doing anything! No error is provided, and the file is not created. However, I noticed that the filePath, which it uses does NOT actually exist (to the best of my knowledge.) This occurs on Android. I have not, yet, tried it on iOS.

build.grade:

buildToolsVersion = "30.0.2" 
minSdkVersion = 21 		
compileSdkVersion = 30 	
targetSdkVersion = 30 		
ndkVersion = "21.4.7075529" 

googlePlayServicesAuthVersion = "19.2.0" 
kotlinVersion = "1.6.0" 

My code:

if (Platform.OS === "android") {
	directoryPath = "Download"; 
}
else {
	directoryPath = "Documents"; 
}

const _createPDF = async () => {
	try {
		let file = await RNHTMLtoPDF.convert({
			html: htmlContents,
			fileName: "myFileName",
			directory: directoryPath, 
		});
		console.log("PDF - file.filePath:", file.filePath, ); // results in the following path: 
		// /storage/emulated/0/Android/data/<myAppName>/files/Download/myFileName.pdf"
		// it should be: /storage/emulated/0/Download/myFileName.pdf
	} 
	catch (err) {
		console.log("PDF - inside _createPDF - catch - err:", err, ); 
	}
};
try {
	await _createPDF(); 
	console.log("PDF - after calling _createPDF successfully"); // <<<<<<<<< The code reaches this point with no errors
} 
catch (err) {
	console.log("PDF - after calling _createPDF - catch - err:", err, ); 
}

Versions:

"react-native": "0.66.4",
"react-native-html-to-pdf": "^0.12.0",

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:7

github_iconTop GitHub Comments

10reactions
Allsterncommented, Apr 14, 2022

If you guys still have an issue concerning the file path and the pdf not getting created in the appropriate folder. I have found a work around.

Go directly to the source code in your node module react-native-html-pdf -> android -> com -> christopherdro -> htmltopdf -> RNHTMLtoPDFModule.java In the convert() function change this code block

File path = (Environment.MEDIA_MOUNTED.equals(state)) ?
          new File(mReactContext.getExternalFilesDir(null), options.getString(DIRECTORY)) :
          new File(mReactContext.getFilesDir(), options.getString(DIRECTORY));

with

File path = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), options.getString(DIRECTORY));

And that should change the file path to the appropriate location inside your emulator/device.

3reactions
Prat1143commented, Feb 18, 2022

Facing the same problem. Path is showing - /storage/emulated/0/Android/data/com.reactnative/files/Documents/test.pdf But no file is being generated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Solved: "There was an error while opening this document.
The path does not exist' when you try to open the PDF from the windows context menu or by double click. Is this...
Read more >
PDF file path is incorrect [duplicate] - java
I want to open a PDF file in my application but something goes wrong everytime. Yeah , I watched many topics about it,...
Read more >
Specified path does not exist
I get the message "The specified path does not exist". All other existing files types are accessible. Only PDF paths seem to be...
Read more >
Error: PDF File not found after compiling
I created my .tex file. I compiled it and the editor tells me that the .log file is created and the PDF file...
Read more >
Excel Macro to Save Sheets As PDF
Export the Active Sheet as PDF File. How the Macro Works. Macro 2 - No Prompt. Macro 3 - File Check. Error -...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found