Suggestion Required PDF On Mail

Ashok Chandra3 months ago

I have updated the my server side code for pdf report, its downloading the pdf file correctly, but when i am sending it to email, so instead of pdf file its sending the excel file, that is corrupted when renaming this file with .pdf so its opening, here is my client side code, please do the needfully.

  const handleSubmit = useCatch(async ({ deviceId, from, to, type }) => {
    const query = new URLSearchParams({ deviceId, from, to });
    if (type === 'export') {
        window.location.assign(`/api/reports/stops/xlsx?${query.toString()}`);
    } else if (type === 'pdf') {
        const response = await fetch(`/api/reports/stops/pdf?${query.toString()}`, {
            headers: {
                'Accept': 'application/pdf'
            }
        });
        console.log("Response:" + response);
        if (response.ok) {
            const blob = await response.blob();
            const url = window.URL.createObjectURL(blob);
            const a = document.createElement('a');
            a.href = url;
            a.download = 'stops_report.pdf';
            document.body.appendChild(a);
            a.click();
            a.remove();
        } else {
            throw Error(await response.text());
        }
    } else if (type === 'mail') {
        const response = await fetch(`/api/reports/stops/mail?${query.toString()}`);
      } else if (type === 'emailPdf') {
        console.log("You are sharing PDF with mail")
        const response = await fetch(`/api/reports/stops/emailPdf?${query.toString()}`, {
          headers: {
              'Accept': 'application/pdf'
          }
      });
        if (!response.ok) {
            throw Error(await response.text());
        }
    } else {
        setLoading(true);
        try {
            const response = await fetch(`/api/reports/stops?${query.toString()}`, {
                headers: { Accept: 'application/json' },
            });
            if (response.ok) {
                setItems(await response.json());
            } else {
                throw Error(await response.text());
            }
        } finally {
            setLoading(false);
        }
    }
});
Ashok Chandra3 months ago

now its resolve