-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
download pdf on custom button click #975
Comments
Also, need this. Right now I'm passing the link to the button text. It's a little hacky. It would be nice to be provided with a trigger that could be passed to onClick handlers. |
Can you please explain how you are passing link to the button text ? |
Default button
With PDF
and then in the
Note that this will generate the pdf when the page loads. If you don't want that you'll need to mess with loading and displaying the link based on if the button is clicked. |
I've accomplished this via some headaches that I'm trying to solve in #972. Here's an example of how to do a custom onClick handler on a button to lazy load the PDF: import { pdf } from '@react-pdf/renderer';
import { saveAs } from 'file-saver';
import React from 'react';
import { DocumentPdf, getProps } from './document-pdf';
export const LazyDownloadPDFButton = () => (
<button
onClick={async () => {
const props = await getProps();
const doc = <DocumentPdf {...props} />;
const asPdf = pdf({}); // {} is important, throws without an argument
asPdf.updateContainer(doc);
const blob = await asPdf.toBlob();
saveAs(blob, 'document.pdf');
}}
>Download PDF</button>
); Feel free to tweak to your needs. I slimmed my code down quite a bit for the example here. However, in a real code base I also track when it is async loading the pdf blob to disable multiple onClick events, and cache the generated blob to speed up subsequent download clicks. Using I hope this helps. |
Not sure if this is what you're looking for, but I was able to resolve the lazy load + custom button using a version of #736. |
Thanks, Is there any chance we can put this block of execution into web worker ? |
Can you please send the related files, e.g document etc, I've to fetch the data in document from api |
Sure, here's a larger form file with the example code I posted above: https://gist.github.com/JacobFischer/aecbd871cb2aae46993236f65797da5c That should help show you how to fetch asynchronous data for your document |
Hey, I've tried your code but there are some issues about passing the data. I am passing the data like (the visible notes are the notes that are shown given proper redux filter and store)
With this I want to pass the dynamic data directly to the pdf file, but it crashes saying this: Error: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead. |
@PauloQuagliatto, according to this comment: https://gist.github.com/JacobFischer/aecbd871cb2aae46993236f65797da5c#gistcomment-3479589 Replace: const asPdf = pdf({}); with: const asPdf = pdf([]); Both hacks work for me. Though perhaps how you are structuring your pdf is similar to that comment. |
This actually worked! Many thanks! |
Update: never mind. Set I have an
|
Thanks a lot, I faced the same problem and finally it worked 🥳 |
@JacobFischer seems like a good solution. Thanks! |
@JacobFischer Thanks a lot. You saved my entire day. Really appreciate. |
Thank you so much!!! |
Man, thank you so much, you saved my code. Did you created a loading behavior? I've put a setLoading(true) and a setLoading(false) inside the onClick, but it doesn't work correctly in a PDF with large data, how can i create a better loading? |
Much appreciated! Worked well for me. I too added the state values to track PDF generation and disable the button. Worked well, though my PDF is not extremely large. |
hey how use it with rtk state? |
this work for me. |
If it can help, here is an example of a basic react button component accomplishing this lazy pdf downloading, with the react-pdf import React, { useState, useEffect } from "react";
import { usePDF, Document, Page, Text } from "@react-pdf/renderer";
// Define the PDF document (you can also import it)
const myPdfDocument = (
<Document>
<Page>
<Text>Hello PDF Document</Text>
</Page>
</Document>
);
/**
* Triggers a download for a given URL by creating a hidden anchor element.
* @param url - The URL of the file to be downloaded.
* @param title - The title to be used for the downloaded file (default is 'document').
*/
function triggerDownload(url: string, title: string = "document") {
const a = document.createElement("a");
a.href = url;
a.download = title + ".pdf";
a.click();
}
export function DownloadPdfButton() {
const [pdfInstance, updatePdfInstance] = usePDF({}); // Instantiate an empty PDF instance using the usePDF hook
const [isDownloading, setIsDownloading] = useState(false);
function initiateDownload() {
setIsDownloading(true);
updatePdfInstance(myPdfDocument); // Update the PDF instance with your document when the download button is clicked
}
// Trigger the PDF download when the button is clicked and the PDF instance URL is available
useEffect(() => {
if (isDownloading && pdfInstance.url) {
triggerDownload(pdfInstance.url)
setIsDownloading(false);
}
}, [isDownloading, pdfInstance.url]);
return (
<button onClick={initiateDownload} disabled={isDownloading}>
{isDownloading ? "Loading..." : "Download PDF"}
</button>
);
} If you have any remarks or suggestions about that I'll be happy to read them, hope this helps ! |
This solution really helped me. Thanks |
hi,
Is there any way i can download my pdf on my custom button click without using in built PdfDownloadLink ?
The text was updated successfully, but these errors were encountered: