Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PDFDownloadLink not working using next js #2744

Open
aminkassim opened this issue May 7, 2024 · 3 comments
Open

PDFDownloadLink not working using next js #2744

aminkassim opened this issue May 7, 2024 · 3 comments

Comments

@aminkassim
Copy link

<PDFDownloadLink
document={<RptInvoice {...reportData} />}
fileName={orderView.invoice_no + ".pdf"}
className="hover:cursor-pointer hover:underline text-blue-500"
>
{({ blob, url, loading, error }) =>
loading
? "Loading document..."
: orderView.invoice_no
}

The code above cant working on my code, why?

@om-albiorix
Copy link

import React, { useState } from 'react';
import { PDFDownloadLink } from '@react-pdf/renderer';
import FormComponent from './FormComponent';
import PdfDocument from './PdfDocument';

const App = () => {
const [formData, setFormData] = useState(null);

const handleFormSubmit = (data) => {
setFormData(data);
};

return (
<div style={{ textAlign: 'center', marginTop: '20px' }}>

Form to PDF



{formData && (
<PDFDownloadLink
document={}
fileName="formData.pdf"
>
{({ blob, url, loading, error }) =>
loading ? 'Generating PDF...' : 'Download PDF'
}

)}

);
};

export default App;

@SammyMusyoki
Copy link

SammyMusyoki commented May 16, 2024

<PDFDownloadLink
className='styles'
document={<PDFComponent {...props}/>}
fileName="example.pdf"
>
{({ blob, url, loading, error }) => {
const handleClick = (event: any) => {
event.preventDefault();

                if (!blob) return;

                const blobUrl = URL.createObjectURL(blob);

                const link = document.createElement("a");

                link.href = blobUrl;
                link.download = "example.pdf";

                document.body.appendChild(link);

                link.click();

                document.body.removeChild(link);
              };

              return (
                <span
                  role="button"
                  onClick={handleClick}
                  className={
                    loadingState === LoadingState.LOADING
                      ? styles["text-disabled"]
                      : ""
                  }
                >
                  Download
                </span>
              );
            }}
          </PDFDownloadLink>
          
          I used this and it worked

@antal-bukos
Copy link

antal-bukos commented May 20, 2024

I ran into this same problem and what I found weird is that it works without Text elements, but when using text loading is never set to false. Based on discussions on a different issue, it happens when using custom fonts in the text, it's caused by a dependency of react-pdf, this helped me #2675 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants