unsupported URL error In expo FileSystem

const handleDownloadMessage = async () => {

const {uri} = await Print.printToFileAsync({

  html:"hii",

  width: 612,

  height: 850,

});

console.log(uri);

const downloadResumable = FileSystem.createDownloadResumable(

  uri,

  FileSystem.documentDirectory + 'small.mp4',

  {},

);

try {

  const a = await downloadResumable.downloadAsync();

  console.log('Finished downloading to ', a);

} catch (e) {

  console.error(e);

}

};

This is giving errors

ANDROID:-Expected URL scheme ‘http’ or ‘https’ but was ‘file’

IOS:-Unable to download file: Error Domain=NSURLErrorDomain Code=-1002 “unsupported URL” UserInfo={NSErrorFailingURLStringKey=file:///var/mobile/Containers/Data/Application/45D991F9-AEFB-4681-BB61-B594DC3836FC/Library/Caches/ExponentExperienceData/%2540rajeshnagaral%252FMFW/Print/6FFC3DA1-B774-40A8-A44D-78F34E5ED092.pdf, NSErrorFailingURLKey=file:///var/mobile/Containers/Data/Application/45D991F9-AEFB-4681-BB61-B594DC3836FC/Library/Caches/ExponentExperienceData/%254ankit%252Fggg/Print/6FFC3DA1-B774-40A8-A44D-78F34E5ED092.pdf,
_NSURLErrorRelatedURLSessionTaskErrorKey=(
“BackgroundDownloadTask <0046BC6C-C542-454B-8EF5-78B8C0F71677>.<1>”
), _NSURLErrorFailingURLSessionTaskErrorKey=BackgroundDownloadTask <0046BC6C-C542-454B-8EF5-78B8C0F71677>.<1>, NSLocalizedDescription=unsupported URL}

Please help

What are you trying to do?

It looks like you’re printing something to file and then download the file you have just created?

@wodin I have to generate a pdf from HTML and save it to the device.

I tried with other npm package like RNHtmltopdf etc but its not working with expo I have to eject the app inorder to use them. Which I can’t.

That’s why I am searching a way to do it.

For future reference, it’s generally better to start with what you’re trying to do before saying how you’re trying to do it :slight_smile:

According to the docs, Print.printToFileAsync() does the conversion to PDF, so it looks like you’re on the right track with that part.

Print.printToFileAsync(options)

Arguments

  • options ( FilePrintOptions ) - A map of print options.

Prints HTML to PDF file and saves it to app’s cache directory. On Web this method opens the print dialog.

Returns

  • Promise<FilePrintResult>

What do you want to do with the file after it has been written?

If you want to move it to your app’s documentDirectory, then you should be able to use FileSystem.moveAsync()

1 Like

This information is out of date if you use EAS Build. I have had a quick look at react-native-html-to-pdf and it looks like it might work out of the box as long as you build with EAS Build. If not you would need to write a config plugin. But since Print.printToFileAsync() looks like it should do the conversion for you it’s probably better to use that.

@wodin Thanks for the response.
Its working with moveAsync().
But I am not able to find the place it is saved also I have to store this in Internal storage Documents Folder. But its getting saved at
Android:-file:///data/user/0/host.exp.exponent/files/ExperienceData/%2540rajeshnagaral%252FMFW/Report
IOS:-
file:///var/mobile/Containers/Data/Application/45D991F9-AEFB-4681-BB61-B594DC3836FC/Documents/ExponentExperienceData/%2540rajeshnagaral%252FMFW/Report

const handleDownloadMessage = async () => {

const pdf = await Print.printToFileAsync({

  html:"HELLLLoooO",

  width: 612,

  height: 850,

});

const newPath = FileSystem.documentDirectory+"Report";

try {

await FileSystem.moveAsync({

  from: pdf.uri,

  to: newPath,

});

} catch (e) {

console.log('Error:', e);

}

console.log(newPath);

};

I have tried with expo build:android it didn’t work but will surely try with EAS build . I have only used EAS to deploy ios to appstore.

Any way to save a pdf to internal storage documents folder using EXPO?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.