How can I use jsPDF to align text in the center of table cells?

Is there a way to center the text in table cells when generating a PDF with jsPdf? By default, the text is left-aligned and I'd like it to be centered.

Best regards, Afroz Ali

Answer №1

If you want, you have the option to transform your file into a PDF format:

let pdf = new jsPDF('p','pt','a4');

pdf.addHTML(document.body,function() {
    pdf.save('file.pdf');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.0.272/jspdf.debug.js"></script>

<div>
  <div style="text-align:center">Greetings from the digital realm!</div> 
</div>

Answer №2

To determine alignment in a PDF, you have the option to set it either within the options or as a parameter, depending on how you are creating the PDF. If you encounter any issues, be sure to share your trial code so that we can help troubleshoot effectively.

 var doc = new jsPDF('p', 'pt', 'a4');
    //Alignment based on page width
    doc.writeText(0, 40 ,'align - center ', { align: 'center' });
    doc.writeText(0, 80 ,'align - right ', { align: 'right' });
    doc.writeText(0, 120 ,'align - left '});

    //Alignment based on text container width
    doc.writeText(0, 120 ,'align - center : inside container',{align:'center',width:100});

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

Dynamically adjust the height of a parent div to match its child div's height upon clicking using JavaScript

Within my div element (divChatContainer), there is a top div (divChatContainerTop) containing a "span" element. When this "span" element is clicked, I want the container div to resize to the height of the "divChatContainerTop" (essentially minimizing the c ...

There seems to be an issue with the post request to a PHP file as it is returning a null value when

I have been struggling with this issue for a while now and can't seem to understand why I keep getting null after calling my $.ajax function. I pass an associative array containing the method name, then invoke the method in PHP to return a JSON string ...

Create a gulp task within a callback function that is initiated by calling the filesystem.readdir

When attempting to read the name of the first folder from a directory, and then defining gulp tasks based on that folder, I am encountering an issue. Specifically, after defining a task inside the callback function, the tasks do not get properly defined. T ...

What is the best way to incorporate a state parameter into a URL when making a GET request with Axios in ReactJS?

Currently, I am utilizing the axios library within a React application to fetch data from a Django backend. I have successfully retrieved user data and stored it in the component's state. However, I now require one of the attributes from the user_data ...

Tips for resolving the setAttribute() function error message: "Argument of type 'boolean' is not assignable to parameter of type 'string'"

I am currently working on a function that dynamically updates the HTML aria-expanded attribute based on whether it is true or false. However, when I declare element as HTMLElement, I encounter an error stating Argument of type 'boolean' is not as ...

Retrieve and update data on-the-fly using XMLHttpRequest from an external API

I am currently dealing with a Web App that refreshes every 5 seconds using a meta tag in HTML. It connects to a web service each time the page is refreshed to display the JSON results. However, I now require this process to occur only when there is a new v ...

What is the best way to transform a single quote within an element of an array, which is part of an object, into a double quote without having to generate a new array

I've been advised to use a specific code for one of the problems I tackled some time ago. I'm attempting to figure it out but so far, I haven't made any progress. The challenge is to accomplish this task using replace(), map() and more withi ...

What is the best way to showcase the contents of an array of objects from JavaScript on an HTML page in a dynamic

Looking for guidance in Javascript as a beginner. For an assignment, I need to create two separate JS files and use them to display data dynamically on an HTML page. One JS file, named data.js, contains an array of objects representing a product catalog. T ...

What is the best way to remove a specific item from a list of maps in DynamoDB based on a particular attribute value within the

Below is an example of a list I have: { "favorites": [ { "createdAt": 1448998673852, "entityId": "558da3de395b1aee2d6b7d2b", "type": "media" }, { "createdAt": 1448998789252, "entityId": "558da3de395b1aee2d6b7d83 ...

What steps can be taken to ensure that only a single decimal separator is included?

I've created a calculator in HTML with two display screens, operators, and number buttons. The challenge I'm facing is implementing a decimal separator. Initially, I attempted to use a boolean variable as a switch, but the number() function remov ...

When navigating to a new route using history.push in React, it's important to ensure that the correct state is

My goal is to implement a smooth exiting animation with framer motion based on the user's current route and next destination. I specifically want the background to slide away only when transitioning from route A to route D. To achieve this, I decided ...

Issue concerning the relative path of RequireJS optimizer

Currently, I am attempting to implement the require optimizer's browser example. My folder structure is set up as follows, with the "r.js" and "build.html" files located at the same level as the "js" folder. js lib | a.js | b.js | ...

Error: The value "'827'" cannot be assigned to "Course_Content.course_outline_id" as it must be a valid instance of "Course_Outline"

While working on my django view, I encountered an error stating: ValueError: Cannot assign '827': 'Course_Content.course_outline_id' must be a 'Course_Outline' instance. I attempted to convert it to an int but it still didn&ap ...

What is the best method for utilizing the jQuery Selector in this particular web application?

I have been attempting to figure out how to select a delete icon within my personal web application. delectIcon HTML <main> <div class="container"> <div class="tabs"> <a href=""><p><span class="act ...

Looking to display an input field when a different option is chosen from the dropdown menu?

I'm currently utilizing ng-if to toggle the visibility of an input box. However, whenever I refresh the page, the input box automatically appears and then hides after selecting an option. Below is my code snippet. Any suggestions would be greatly appr ...

Generate a new entity by merging two objects containing keys that are identified by dots

I have a unique input as shown below: { survey.subObject1.key1: true, survey.subObject1.key2: "OTHER", survey.subObject2.key3: "[1,2]", survey.subObject2.key4: false, survey2.subObject3.key5: false, survey2.subObject3.key6: false } I a ...

Choose a specific range of dates using only one Bootstrap Datepicker

Currently utilizing Bootstrap DatePicker version 1.8.0 from this repository. I am looking to select a range within a single Bootstrap DatePicker and require the input values to be sorted in ascending order (as shown in the example below). https://i.sstat ...

Unable to retrieve the initial return value from the function that has been promisified

Previously, the code functioned correctly but was not properly promisified. const express = require('express'); function runServerAndReturn() { app = express(); console.log('Before starting server'); const server = app.lis ...

What is the best way to send data back to a separate JavaScript file in ExtJS when working with records in a

I'm currently working on implementing a pop-up editing feature on a grid in extjs4. Progress so far includes successfully transferring the grid record to a popup panel located in a separate JavaScript file using the following code: handler: function( ...

Having trouble locating elements within an iframe on Safari while employing Protractor

Our webpage includes an iframe that is dynamically generated using JavaScript. We are working on creating end-to-end tests for this page using Protractor. Our specific goal is to verify the presence of a div element inside the iframe. The issue we are en ...