Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
922 B
2
Indexable
Never
const [pdfUrl, setPdfUrl] = useState("");

  const handleGeneratePdf = async () => {
    try {
      const response = await axios.post(
        "http://localhost:3000/v1/api/orders-pdf",
        {
          orderId: "657c2ca30c99005d1404a55f", // Replace with the actual order ID
        },
        {
          headers: {
            "Content-Type": "application/json",
          },
          responseType: "blob",
        }
      );

      const blob = new Blob([response.data], { type: "application/pdf" });
      const pdfUrl = URL.createObjectURL(blob);
      setPdfUrl(pdfUrl);
    } catch (error) {
      console.error("Error generating PDF:", error);
    }
  };

 return (
    <div>
      <button onClick={handleGeneratePdf}>Generate PDF</button>
      {pdfUrl && (
        <div>
          <embed src={pdfUrl} width="800px" height="600px" />
        </div>
      )}
    </div>
  );
Leave a Comment