In the app I am working on rightnow I want to CREATE a PDF out of html with Print.printToFileAsync() and safe it to the local storage.
It’s working well but I need to give the pdf a specific name like: “invoice12345.pdf”.
Is that possible somehow? I could not find anything about it in the forum.
Yes, I found a solution that is working for me!
Basically I also imported the FileSystem API. After receiving the uri from printToFileAsync I manipulate just the very end to the pdf name I want it to have (in this case “invoice_”) and “move” the PDF to that location.
Hope that helps!
import * as Print from 'expo-print'
import * as Sharing from 'expo-sharing'
import * as FileSystem from 'expo-file-system'
const printToPdf = async () => {
const response = await Print.printToFileAsync({
html: createHtmlStringForPdf(),
})
// this changes the bit after the last slash of the uri (the document's name) to "invoice_<date of transaction"
const pdfName = `${response.uri.slice(
0,
response.uri.lastIndexOf('/') + 1
)}invoice_${readableDate.getTime()}.pdf`
await FileSystem.moveAsync({
from: response.uri,
to: pdfName,
})
sharePdf(pdfName)
}
const sharePdf = (url) => {
Sharing.shareAsync(url)
}