The issue with jspdf is that it is failing to generate PDF documents of

I'm currently developing a resume builder app using ReactJS. One of the functionalities I'm working on is enabling users to download their resumes as PDFs. However, I've encountered an issue with the generated PDFs when using jsPDF. The downloaded PDF appears zoomed in or with excessively large font sizes.

To address this problem, I have set the width to 210mm and height to 297mm for the HTML element that will be converted into a PDF. Below is the code snippet I'm using to generate and download the PDF:

function handleDownload() {
    const doc = new jsPDF('p', 'mm', [297, 210]);
    const content = pdfRef.current;
    doc.html(content, {
      callback: function (doc) {
        doc.save('newFile');
      }
    });
  }

Here is the JSX element that I want to be downloadable as a PDF:

<main id="classic" ref={pdfRef} className='mx-auto bg-white shadow w-[210mm] h-[297mm]'>
  <button onClick={handleDownload} className="bg-mainBlack text-white px-4 py-2 text-xs">download</button>
  {/* Rest inner elements */}
</main>

This screenshot shows how the resume looks in the browser (ignore the lack of styling as it's not finalized):

https://i.stack.imgur.com/vfFc4.png

Additionally, here is a comparison of how jsPDF generates the PDF:

https://i.stack.imgur.com/2trLi.png

I am seeking a solution to ensure the PDF is generated at the correct size without distortion.

Answer №1

One way to achieve this is by utilizing the htmltocanvas library.

async function handleDownload() {
    const contentCanvas = await html2canvas(document.getElementById("content"));
    const image = contentCanvas.toDataURL("image/png");
    var doc = new jsPDF();
    doc.addImage(image, "png", 0, 0);
    doc.save("resumse.pdf");
  }

Experience it in action on Code Sandbox : https://codesandbox.io/s/elated-joana-veix3x?file=/src/App.js:184-452

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When sorting items, the page automatically jumps to the top of the screen

I have been working on creating a sortable portfolio using jQuery. One issue I am facing is that every time I click on a filter for the portfolio items, the page automatically scrolls to the top. Is there a way to prevent this from happening? You can vi ...

Differences between addEventListener and jQuery's on method, as well as form.submit and jQuery's

I encountered a situation where callbacks registered using jQuery's on method were not being called when trying to trigger form submission with the HTML Form element object instead of jQuery. After some testing, I discovered that changing the code to ...

What are some creative ways to incorporate images into websites outside of a ReactJS environment?

I'm currently working on a ReactJS application that is client-side only and intended for local use. One of the requirements is to save and load images using filepaths, including URLs and local file system paths. While I am able to store paths in loca ...

In React Js, the Owl Carousel is able to extract data directly from an array for seamless

Hey everyone, I'm working on rendering owl carousel images and content data sourced from an array. Here's a snippet of the data array: const MOVIEBANNER = [ { id: 1, genres: "ACTION / ADVENTURE", language: "ENGLISH, HINDI", ...

React Jodit Editor experiencing focus loss with onchange event and useMemo functionality not functioning properly

I'm currently working on a component that includes a form with various inputs and a text editor, specifically Jodit. One issue I've encountered is that when there are changes in the Jodit editor's content, I need to retrieve the new HTML va ...

The website does not display properly on larger screens, while showing a scrollbar on smaller screens

I'm encountering an issue with my website where I can't seem to find a solution no matter what I try. My goal is to ensure that my website looks consistent across all computer screen sizes. However, when viewed on larger screens, there's a ...

Can JQuery's 'unslider' be customized to only change the backgrounds?

Check out my source at this link: http://codepen.io/anon/pen/Bvkjx Observe how the content and background images rotate correctly. Now, I'm curious if it's feasible to keep the following content static <p>SOME CONTENT1</p> while ...

Challenges encountered with the "load" event handler when creating a Firefox Extension

I am currently troubleshooting a user interaction issue with my Firefox extension. The tasks that my extension needs to complete include: Checking certain structures on the currently viewed browser tab Making backend server calls Opening dialogs Redirect ...

Refreshing an AJAX call automatically in Knockout JS

Greetings everyone! I'm currently working on setting up a simple setInterval function to automatically refresh my data every minute. The line that is giving me trouble is: setInterval(incidentViewModel.fetchdata,60000); I also attempted this: windo ...

"Utilizing the 8-column layout feature in Twitter Bootstrap

Is it possible to set up 8 equal columns using the latest version of Twitter bootstrap? I know how to create 4 equal columns but I am struggling with figuring out how to achieve 8: <div class="col-sm-12"> <div class="row"> ...

Increase transparency of scrollbar background

Is it possible to adjust the opacity of a scrollbar track? I attempted to use opacity:0.5, but it didn't have any effect. I am using material UI styled, although I don't believe that is causing the issue. const Root = styled('div')(({ ...

Closures are like the "use" keyword in PHP or the capture list in C++, but they play a similar role in JavaScript and other transpiler languages

PHP has a useful feature with the use keyword, which allows for the usage of 'external' variables in closures. For example: $tax = 10; $totalPrice = function ($quantity, $price) use ($tax){ //mandatory 'use' return ($price * $quan ...

What is the process for incorporating Dark Mode into a map?

Hey there! I'm diving into the world of web development and experimenting with a new react google maps package. I used npm i @vis.gl/react-google--maps to install it. I'm curious if there's a way to switch the embedded google map on my webs ...

angularJS controller does not seem to be functioning properly due to a certain

var today = new Date(); var dd = today.getDate(); var mm = today.getMonth() + 1; //January is 0! var yyyy = today.getFullYear(); var currentdate = yyyy + ',' + mm + ',' + dd; var appointmentDate = moment($scope.AppointmentsList[i].J).f ...

Can you explain the distinction between onKeyUp and onKeyUpCapture in React (as well as onKeyDown/Capture)?

I was unable to find any existing documentation or questions related to my query, which surprised me. Upon inspecting the React index.d.ts file, I came across the following: // Keyboard Events onKeyDown?: KeyboardEventHandler<T>; onKeyDownCapture?: ...

Once more baffled by the nested boxes, this iteration adorned in vibrant hues and trimmed with a striking border

In a previous inquiry, I sought advice on centering one box within another. However, my attempts this time around have led to some peculiar results. I experimented with two different code variations- one involving a background color and the other utilizing ...

Saving information in node.js

My latest project involves creating an address book app using HTML, CSS, and JavaScript. The company provided me with a zip file containing the necessary resources to implement the app using node.js. However, my knowledge of node.js is limited and I have ...

AngularJS returns an empty array following a get request

Upon sending a GET request in my code example to retrieve a response array containing data, I noticed that the array appears empty in the console of Firefox. I am uncertain about where the error might be occurring. https://i.stack.imgur.com/aRWL9.jpg Belo ...

The Laravel return view function seems to be malfunctioning as it is displaying an error page instead

I am facing an issue where I am trying to display an existing view but instead of the show page, I keep getting a "404 not found" error. As a beginner in Laravel, I am still trying to grasp its concepts. Any insights into why I am encountering the error 40 ...

"Using jQuery's animate() function to dynamically change text content

I'm currently venturing into the world of jQuery animate() and decided to create a presentation-style project for practice. However, I've hit a roadblock when attempting to alter the text within my div element in the midst of an animation utilizi ...