react-to-pdf icon indicating copy to clipboard operation
react-to-pdf copied to clipboard

image is not rendering in image removed when pdf downloaded

Open itsshajad opened this issue 3 years ago • 6 comments

itsshajad avatar Oct 13 '21 03:10 itsshajad

The title is confusing and there is no information but I am having this same problem.

I am including an image in an <img /> tag that displays well in my component. Then when I click the download button, the layout still holds but there is no image, just empty space.

Are we able to include images in our PDFs?

oliversentact avatar Jan 31 '22 21:01 oliversentact

The title is confusing and there is no information but I am having this same problem.

I am including an image in an <img /> tag that displays well in my component. Then when I click the download button, the layout still holds but there is no image, just empty space.

Are we able to include images in our PDFs?

I'm having the same problem

crscaballero avatar Feb 04 '22 20:02 crscaballero

I am also facing same issue.

Tushar-Yadav-TCPL avatar Feb 24 '22 09:02 Tushar-Yadav-TCPL

Same issue

SinaSaintZero avatar Mar 24 '22 14:03 SinaSaintZero

hello, @SinaSaintZero @Tushar-Yadav-TCPL @crscaballero @oliverterrell @itsshajad I am currently experiencing this and wanted to know if any of you were able to find a solution? 🙂

Amaka202 avatar Jun 29 '22 13:06 Amaka202

@Amaka202 Unfortunately I was not. Due to time constraints I had to pivot to another library instead of taking a ton of time to figure this issue out. I used react-pdf https://react-pdf.org/ instead, which was workable. I don't like react-pdf because it doesn't let you use anything but its own component library, which is super inconvenient and consumes a lot more time than using any old JSX, but with that said it really is a powerful library

oliversentact avatar Jun 29 '22 13:06 oliversentact

This is how I fixed it. If your image is from a remote URL it will not show. First allow CORS (if working with different hosted backend and frontend). Then covert your remote url to local url at frontend side. This issue occurs as remote URL takes time to process and pdf is generated before the image is fetched. So you have to make sure the image is loaded before converting to pdf.

 async function fetchImage() {
            const imageUrl = 'https://i.imgur.com/7obGLSR.jpeg';
            if (!imageUrl || imageUrl.length === 0) return;
            try {
                const response = await fetch(imageUrl);

                if (!response.ok) {
                    throw new Error(
                        `Image fetch failed with status: ${response.status}`,
                    );
                }

                const blob = await response.blob();
                const blogToLocalURL = URL.createObjectURL(blob);
            } catch (error) {
                console.error('Error fetching pdf image:', error);
            }
        }

abdullahqureshi5050 avatar Aug 16 '23 01:08 abdullahqureshi5050

Not sure if that matters now due to the time elapsed, sorry, but on V1 maybe using the imageTimeout option from htm2canvas might be a way out.

import generatePDF, { Options } from 'react-to-pdf'

const options: Options = {
   filename: 'test.pdf',
   overrides: {
       canvas: {
           imageTimeout: 10000
      }
   }
}

const Component = () => {
   const targetRef = useRef();
   return (
      <div>
         <button onClick={() => generatePDF(targetRef, options)}>Download PDF</button>
         <div ref={targetRef}>
            Content to be included in the PDF
         </div>
      </div>
   )
}

ivmarcos avatar Sep 07 '23 20:09 ivmarcos

Closing this, but feel free to open another ticket if the problem persists with the newest releases.

ivmarcos avatar Sep 08 '23 12:09 ivmarcos