Skip to content

Commit

Permalink
fix: fixed the downalod issue. pick the already geenrated data fro if…
Browse files Browse the repository at this point in the history
…ram and creating a blob url to download the file
  • Loading branch information
rajesh-jonnalagadda committed Jun 4, 2024
1 parent 5efe4e4 commit 05b210a
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions app/components/booking/generate-booking-pdf.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "react";
import { useRef, useState } from "react";
import type { Asset, Booking } from "@prisma/client";
import { Button } from "~/components/shared/button";
import {
Expand Down Expand Up @@ -26,6 +26,7 @@ export const GenerateBookingPdf = ({
timeStamp: number;
}) => {
const [iframeLoaded, setIframeLoaded] = useState(false);
const iframeRef = useRef<HTMLIFrameElement>(null); // Add ref for the iframe
const totalAssets = booking.assets.length;
const url = `/bookings/${booking.id.toString()}/generate-pdf/booking-checklist-${new Date()
.toISOString()
Expand All @@ -38,6 +39,27 @@ export const GenerateBookingPdf = ({
window.location.href = url;
};

const handleDownload = (e: React.MouseEvent<HTMLButtonElement>) => {
try {
e.preventDefault();
const iframe = iframeRef.current;
if (iframe && iframe?.contentDocument) {
const pdfData = iframe?.contentDocument?.body?.innerHTML; // Adjust if necessary to access PDF data
const blob = new Blob([pdfData], { type: "application/pdf" });
const downloadUrl = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = downloadUrl;
a.download = `booking-checklist-${new Date()
.toISOString()
.slice(0, 10)}.pdf`;
a.click();
URL.revokeObjectURL(downloadUrl);
}
} catch (err) {
//do nothing for now.
}
};

return (
<>
<AlertDialog>
Expand Down Expand Up @@ -73,6 +95,7 @@ export const GenerateBookingPdf = ({
<div className={tw(iframeLoaded ? "block" : "hidden", "h-full")}>
<iframe
id="pdfPreview"
ref={iframeRef}
width="100%"
height="100%"
onLoad={handleIframeLoad}
Expand All @@ -91,11 +114,9 @@ export const GenerateBookingPdf = ({
<Button
to={url}
variant="secondary"
role="link"
download={true}
reloadDocument={true}
disabled={!iframeLoaded}
icon="download"
onClick={handleDownload}
>
Download
</Button>
Expand Down

0 comments on commit 05b210a

Please sign in to comment.