How to download web pages as PDF in React JS

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 เม.ย. 2023
  • In this tutorial, you will learn how to add a feature to your ReactJS application that allows users to download web pages as PDF files using html2canvas and jspdf libraries.
    Firstly, you will install and set up the necessary libraries, including html2canvas, which is used to capture the HTML content of the web page as an image, and jspdf, which is used to convert the captured image into a PDF file.
    Next, you will create a React component that captures the content of the web page using html2canvas and converts it into a base64 encoded image. You will then use jspdf to create a PDF document from the captured image and provide a download link for the user to save the file.
    Throughout the tutorial, you will learn how to handle different scenarios, such as when the web page has multiple pages or when the captured image is too large, by splitting the image into multiple parts and combining them into a single PDF document.
    By the end of this tutorial, you will have a complete understanding of how to download web pages as PDF files in ReactJS using html2canvas and jspdf libraries. This tutorial is suitable for anyone with a basic understanding of ReactJS and JavaScript.

ความคิดเห็น • 56

  • @user-vr7ku2rw3p
    @user-vr7ku2rw3p 10 หลายเดือนก่อน +1

    I love simple short tutorials that work. Thank you for this. Worked first time without any issues.

  • @ajayprakashpp7229
    @ajayprakashpp7229 9 หลายเดือนก่อน

    I don't know how to express my gratitude to this video. You have given solution for a problem on which i have been wandering around for 2 days. Thank you ♥

  • @anthonyezeh7511
    @anthonyezeh7511 7 หลายเดือนก่อน

    I was able to integrate it following your steps. Everything works as instructed.

  • @olufemiajibade
    @olufemiajibade 7 หลายเดือนก่อน

    Thanks for this, worked at first try 💯

  • @skazzp
    @skazzp ปีที่แล้ว +1

    Great guide, everything worked from the first try, thank you

  • @abhishekmeena2985
    @abhishekmeena2985 11 หลายเดือนก่อน

    thank you, it was helpful

  • @showhow6827
    @showhow6827 9 หลายเดือนก่อน +1

    Great tutorial, simple and to the point. One major issue though. When the content is longer than the viewport or wider and or even when the you have the window resize to smaller one then the it will not convert the full content. Does anyone have an idea about solving this issue?

  • @rukaiyajahan1172
    @rukaiyajahan1172 ปีที่แล้ว +2

    Thank you so much. such a helpful video . Go ahead, all the best

    • @cricketfan3322
      @cricketfan3322 ปีที่แล้ว

      Ph er student naki?

    • @rukaiyajahan1172
      @rukaiyajahan1172 ปีที่แล้ว

      @@cricketfan3322 yes 🤔

    • @cricketfan3322
      @cricketfan3322 ปีที่แล้ว

      @@rukaiyajahan1172 amio same reason e ashchilam ekhane 😌😇

    • @cricketfan3322
      @cricketfan3322 ปีที่แล้ว

      @@rukaiyajahan1172 hmm done

  • @hardikdangodara3272
    @hardikdangodara3272 6 หลายเดือนก่อน +1

    thank you so much sir

  • @abelmurua6980
    @abelmurua6980 8 หลายเดือนก่อน

    Thank you so much! Since I am using AWS ElasticBeanslkt Amazon Linux 2023, I couldn't use Puppeteer because of their environment its based on 64 bits. This solved my PDF need, I spent 2 days trying to find a solution, I appreciate your input!! thank you!
    The only thing I noticed is that TABLE layout its somehow off, not showing the same thing as it does on browser. th (headers) had some top padding on the cell, or the titles it self are alinged to bottom. I tried with lots of tailwindcss rules to reset that but I couldn't make it work, everything else looked great! I just removed the bg color so you cant tell there is a ht space.! Thank you again

  • @billyjoshuasalcedo3955
    @billyjoshuasalcedo3955 ปีที่แล้ว

    how can you optimize the downloading time? I'm having trouble into downloading the pdf because it takes around 5 seconds before downloading the pdf. I'm using useRef for the DivElement and is passing down the scale to 1 to lower the graphics but to no avail.

  • @user-nl5zc6kc9i
    @user-nl5zc6kc9i 10 หลายเดือนก่อน

    Hi bro, thank you so much, i just have a little problem when i download the PDF my images dont render in it

  • @vivekjaiswal2422
    @vivekjaiswal2422 7 หลายเดือนก่อน +2

    I have to download a scrollable div, but by using the above same logic I am unable to do it.
    I am getting only the current view port in pdf.☹

  • @lubomirsevcik2252
    @lubomirsevcik2252 6 หลายเดือนก่อน

    thx man

  • @kanishkakodithuwakku1944
    @kanishkakodithuwakku1944 12 วันที่ผ่านมา

    thanx brother

  • @kanibis811
    @kanibis811 ปีที่แล้ว

    thank you @code pro, that's what I needed. I wanted to know about the step guide that you created - have you used animation for rendering the steps one-by-one? Please do reply, I'm in the learning phase and it'd really help me. Also can you please share step-guide's github link?

    • @CodePro-Jayanth
      @CodePro-Jayanth  ปีที่แล้ว

      Thank you, actually I have created animation for rendering step by step

  • @user-pv2tx5np2g
    @user-pv2tx5np2g 10 หลายเดือนก่อน +2

    how to take print long web page ? more than 1 page

  • @minhtringuyen1267
    @minhtringuyen1267 6 หลายเดือนก่อน

    I have issue with the tag svg it's not render correctly, can u help me about this issue.

  • @sunidhichouhan4103
    @sunidhichouhan4103 11 หลายเดือนก่อน +1

    how can we make text selectable in pdf
    ? please help me oue.

  • @MaharajanRavi
    @MaharajanRavi 8 หลายเดือนก่อน

    is there any other method to download as pdf with support svg images?

  • @stan-beats
    @stan-beats ปีที่แล้ว

    thx

  • @niveditasingh7691
    @niveditasingh7691 6 หลายเดือนก่อน

    If I need to add link to pdf, how does that work with this approach? @code pro

  • @yeshthummer7256
    @yeshthummer7256 9 หลายเดือนก่อน

    can i share a link using this package and save link and after save the pdf and open link from pdf ??

  • @SanjaySanjay-zg1jy
    @SanjaySanjay-zg1jy 5 หลายเดือนก่อน

    I want to download in a new tab. How to do using ur code

  • @shubhamdas6519
    @shubhamdas6519 8 หลายเดือนก่อน

    what if i am rendering the image from online link?

  • @g3zeck485
    @g3zeck485 5 หลายเดือนก่อน

    Helo i have this error "incomplete or corrupt png file" Does anyone know the solution?

  • @tamimhossain4141
    @tamimhossain4141 2 หลายเดือนก่อน

    great but how I download multiple page

  • @anik1612
    @anik1612 ปีที่แล้ว +1

    informative

  • @rahulmohanty3822
    @rahulmohanty3822 13 วันที่ผ่านมา

    what is the font name ?

  • @varadtote2017
    @varadtote2017 10 หลายเดือนก่อน

    I got it right but image quality is too bad

  • @abdulragib6004
    @abdulragib6004 11 หลายเดือนก่อน

    i follow same approach but it didn't work

  • @kimothe2nd
    @kimothe2nd 15 วันที่ผ่านมา

    That did NOT download the cv..

  • @user-ni9cr7iq4u
    @user-ni9cr7iq4u 7 หลายเดือนก่อน

    scrollable content not capturing

  • @DeveloperInside
    @DeveloperInside 8 หลายเดือนก่อน

    Can we select text and copy in generated pdf

    • @CodePro-Jayanth
      @CodePro-Jayanth  8 หลายเดือนก่อน +1

      It's not possible in this approach, for that we need to use react-pdf library. In few days I will post the video for that

  • @kleatech6562
    @kleatech6562 9 หลายเดือนก่อน +1

    please can you share the github link or the blog link

    • @jorgegarnicablanco8136
      @jorgegarnicablanco8136 8 หลายเดือนก่อน

      const downloadPDF = () => {
      const input = quoteRef.current;
      html2canvas(input as HTMLElement).then((canvas) => {
      const imgData = canvas.toDataURL('image/png');
      const pdf = new jsPDF('p', 'mm', 'a4', true);
      const pdfWidth = pdf.internal.pageSize.getWidth();
      const pdfHeight = pdf.internal.pageSize.getHeight();
      const imgWidth = canvas.width;
      const imgHeight = canvas.height;
      const ratio = Math.min(pdfWidth / imgWidth, pdfHeight, imgHeight);
      const imgX = (pdfWidth - imgWidth * ratio) / 2;
      const imgY = 30;
      pdf.addImage(
      imgData,
      'PNG',
      imgX,
      imgY,
      imgWidth * ratio,
      imgHeight * ratio
      );
      pdf.save('cotizacion.pdf');
      });
      };

  • @rodrigocastro3934
    @rodrigocastro3934 ปีที่แล้ว +4

    This is not the correct approach. This is only converting the web into an image, and then paste it on a pdf. Consequently: final pdf has no text you can select and/or copy, it is just an image; and second, if you have several pages this method does not work. Does anyone has a correct solution? Please leave it bellow 🙏🏻

    • @CodePro-Jayanth
      @CodePro-Jayanth  ปีที่แล้ว +1

      With this library you can create multi page pdf as well

    • @user-ni9cr7iq4u
      @user-ni9cr7iq4u 7 หลายเดือนก่อน

      @@CodePro-Jayanth how

    • @varunsalat619
      @varunsalat619 6 หลายเดือนก่อน

      yes how? I was creating a dynamic invoice pdf that's why came here..

  • @shuvomajumder5565
    @shuvomajumder5565 6 หลายเดือนก่อน +1

    I got an error. Uncaught (in promise) Error: Attempting to parse an unsupported color function "oklch".

  • @khaouitiabdelhakim
    @khaouitiabdelhakim 9 หลายเดือนก่อน

    Thanks alot. th source code pleaaaaaaaaaaaaaaase!

  • @roufhasan
    @roufhasan 7 หลายเดือนก่อน

    I am facing this error: Uncaught (in promise) Error: Attempting to parse an unsupported color function "oklch"
    html2canvas.js?v=82c7c37b:1685

  • @lasith123
    @lasith123 5 หลายเดือนก่อน +2

    const handleDownloadPDF = () => {
    const input = pdfRef.current;
    html2canvas(input).then((canvas) => {
    const imgData = canvas.toDataURL('image/png');
    const pdf = new jsPDF('p', 'mm', 'a4', true);
    const pdfWidth = pdf.internal.pageSize.getWidth();
    const pdfHeight = pdf.internal.pageSize.getHeight();
    const imgWidth = canvas.width;
    const imgHeight = canvas.height;
    const ratio = Math.min(pdfWidth / imgWidth, pdfHeight / imgHeight);
    const imgX = (pdfWidth - imgWidth * ratio) / 2;
    const imgY = 30;
    pdf.addImage(
    imgData,
    'PNG',
    imgX,
    imgY,
    imgWidth * ratio,
    imgHeight * ratio,
    );
    pdf.save('invoice.pdf');
    });
    };

  • @billyjoshuasalcedo3955
    @billyjoshuasalcedo3955 ปีที่แล้ว +2

    how can you optimize the downloading time? I'm having trouble into downloading the pdf because it takes around 5 seconds before downloading the pdf. I'm using useRef for the DivElement and is passing down the scale to 1 to lower the graphics but to no avail.

    • @miakablan8792
      @miakablan8792 10 หลายเดือนก่อน

      can you use workers ?