Expo Print - creating PDF and giving it a FILE NAME

Hello everyone!

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.

That’s all I have done so far:

    async function printToPdf() {
        const response = await Print.printToFileAsync({ html: '<h1>Test-Invoice</h1>' })
        console.log(response.uri)
        sharePdf(response.uri)
    }

Many thanks in advance!!

1 Like

Hi Kenobivan,
Have you figured it out? I am facing the same problem.

Hi narayan,

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)
    }
3 Likes

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