Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify the name of pdf print for react native project for iOS

I'm using both expo-print and expo-sharing to save file.

const { uri } = await Print.printToFileAsync({html});
await shareAsync(uri, { UTI: ".pdf", mimeType: "application/pdf" });

by default it is using UUID, I want to specify the file eg, abc.pdf, but I don't see the doc has any option to setup the file name.

like image 862
Weijing Jay Lin Avatar asked Oct 24 '25 17:10

Weijing Jay Lin


1 Answers

I found the answer here

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)
}
like image 57
Gustavo Soares Martins Avatar answered Oct 28 '25 04:10

Gustavo Soares Martins



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!