The jsPDF library creates PDFs with elements that are stretched and distorted

I am currently exploring how to use Html2Canvas in combination with jsPDF to create a PDF from HTML content. I am following a tutorial that guides me through the process.

My goal is to render a basic Hello World h1, and while I do get the PDF with the text, it appears stretched out. I'm struggling to adjust it properly for correct display. You can see the issue in the image below:

https://i.sstatic.net/eFCp2.png

I have attempted altering the PDF object configuration like so: pdf.internal.scaleFactor = 2.25;

However, this adjustment doesn't seem to have any effect. I've tested other configuration changes without success as well.

<template>
  <div>
    <a @click="print">Print</a>
    <div id="nodeToRenderAsPDF">
      <h1>Hello World</h1>
    </div>
  </div>
</template>

<script>
import jsPDF from 'jspdf'
import html2canvas from 'html2canvas'

export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  methods: {
    print() {
      const filename  = 'ThisIsYourPDFFilename.pdf';

      html2canvas(document.querySelector('#nodeToRenderAsPDF')).then(canvas => {
        let pdf = new jsPDF('p', 'mm', 'a4');
        pdf.internal.scaleFactor = 2.25;
        pdf.addImage(canvas.toDataURL('image/png'), 'PNG', 0, 0, 211, 298);
        pdf.save(filename);
      });
    }
  }
}
</script>

The output I'm seeing is the distorted element within the PDF. Ideally, I would like to achieve a PDF that displays "Hello World" in the same size as the h1 tag shown on the browser.

Answer №1

After encountering issues with jspdf, I decided to switch to pdfmake and found it to be a fantastic library. I highly recommend giving it a try!

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

``Why is my Vue.js ag-Grid filter component failing to function?

I have recently started working with vue-js and ag-grid, and I wanted to implement a custom filter on my ag-grid. I attempted to use a component as a filter based on the example provided in the vue-js ag-grid documentation: "https://www.ag-grid.com/javascr ...

Using JavaScript to toggle the display of a label element

Greetings everyone! I recently posted a question on this thread about replacing input with javascript, but ended up abandoning that idea. Instead, I decided to explore a different approach... I made the background of my password field transparent and posi ...

What is the best way to enable and disable the edit-in-place feature using jQuery?

Recently, I decided to experiment with the jQuery In Place Editor but I've come across an issue I can't seem to solve. Below is the code snippet that I have been working on. In my implementation, I am utilizing the jQuery toggle method to enable ...

Tips for simulating an ajax request in a Jasmine test

Check out my code snippet below: function sendRequestData(url, urlParameters) { $.ajax({ url : url, method : 'POST', headers : { 'Accept' : 'application/json' }, contentType : 'application/js ...

When using Mongoose's updateOne, the existing nested documents array is replaced entirely rather than just appending a new document to

I am facing an issue when trying to add a new nested Item document to a documents array in the List document. Instead of adding the Item document to the array, it ends up overwriting the existing documents with the new one. Below is the snippet of code ca ...

Utilizing Node.js to import files without the use of namespaces

I am in the process of transitioning a codebase from being entirely browser-based to incorporating both server-side and client-side functionalities. One major challenge I have encountered is the need to use modules in order for my code to run smoothly usi ...

Issue: Unable to locate dependencies for modules Vite and react

Struggling to develop a react app using vite and encountering the following error message: Error: Failed to scan for dependencies from entries: C:/Users/User/Desktop/responsive-app/index.html The path to my project, I am using windows for this specific p ...

Every time I attempt to send a post request, I receive back only the creation date and the unique objectID

Whenever I send a post request, only the created date and objectID are returned. Whenever I send a post request, only the created date and objectID are returned. This issue persists even after multiple attempts. I attempted to verify it using Postman ...

The axios POST request successfully returns a status code of 200, but unfortunately, the database

https://i.sstatic.net/FYO7k.png I have encountered an issue with my code where I am able to receive a response with status 200, however, the database is not updating. The backend has indicated that it received null data. axios.post('api/nos ...

Tips for displaying res.locals information in the console for debugging purposes using ExpressJS and Nodejs

Currently, I am delving into the world of Node.js and ExpressJS as a novice in this realm. My primary requirement is to log the entire or partial content of res.locals in my console for debugging purposes. Here's what I've attempted: var foo_ro ...

Retrieving elements from the DOM using jQuery

UPDATE: JSFIDDLE link. My goal is to synchronize elements from DESKTOP 1 to MOBILE 1 (same for #2) in the fiddle. The error is appearing in the console. Go to DESKTOP 1 and choose NEW RATE from the dropdown list. Keep the console open to observe the issu ...

Issue with Bootstrap 4 nested collapse: "data-parent" attribute malfunctioning

I'm looking to implement the collapse data-parent options in a way that mimics traditional accordion behavior: all collapsible elements under a specified parent should close when a new one is opened. However, I am encountering issues with this setup. ...

How to Ensure a Component is Loaded Only Once in React JS

Currently, I am working on a React App with two pages - Home and My Portfolio Section. The Portfolio Section consists of two functional components. Home.js import React from 'react' const Home = () => { return ( <h1>Home Screen&l ...

Set focus on Bootstrap modal when closing the popup near the input field within the body

Hello fellow members of the Stack Overflow community! I'm trying to make sure that when users click on a Bootstrap modal window, the input field automatically gets focused. I've attempted the code below but it's not working as expected. Any ...

Preserve all the impact of a website indefinitely

Imagine a scenario where a webpage contains a button that, when pressed, creates another button below it. How can you save this effect so that when you reopen the webpage, the new button is still displayed below the original one? Is there a way to store ...

When attempting to send an email using the emailjs.send function, an unexpected error occurred showing the URL https://api.emailjs.com/api/v1.0/email/send with a

import emailjs from 'emailjs-com'; async function sendEmail() { try { const serviceID = '...'; const templateID = '...'; const userID = '...'; const emailParams = { to_email: '...&a ...

Using setInterval in pugjs

Trying to implement a time creator using mixins in PugJS. I am getting special names from other pages as "type" but encountering an unexpected text error on the second to last line. I have been unable to resolve it, could you please assist? mixin createT ...

Retrieving customData from fullCalendar JSON in the dayClick event handler

Apologies for what may be a simple question. I am trying to insert some custom JSON data into the fullCalendar plugin and utilize it within my dayClick callback function. Specifically, I aim to access and modify elements in customData when a day is clicked ...

send a variable to a function in a random sequence

In my code, I have a function defined as follows: let showNotification = function(a,b,c,d,e,f){ console.log(a,b,c,d,e,f); } When calling this function, it is crucial to pass parameters in the correct order. For example, if I want to omit values for c, ...

Error: Unable to access undefined properties (reading 'url')

I am currently working on creating a drag-and-drop card game and I have encountered an issue with the react-dnd library. When using data from the file, everything works fine, but if I have to fetch the data externally, it throws an error. This problem see ...