Create a list of items with checkboxes next to each that can be repeated using PdfMake

Incorporating pdfMake into my project, I am trying to display text next to an image and replicate this section in my docDefinition. The issue arises when I attempt to repeat this part using the following code snippet:

{
      columns: [
        {
          image: "checked",
          height: 10,
          width: 10
        },
        {
          stack: [
            {
              columns: [
                {
                  text: 'First column first line',
                  width: '50%',
                  margin: [5, 0, 0, 0],
                }
              ]
            }
          ],
          width: '*'
        }]
    }

Within my docDefinition:

let docDefinition = {
  pageSize: 'LEGAL',
  pageMargins: [40, 80, 40, 60],
  content: [
    {
        ...
        this.getFailureLocationObject(),
        ...
    }

         
};

pdfMake.createPdf(docDefinition).download('Intervention' + '19060023');

However, when attempting to populate an object list (ol) with similar image-text combinations using the function getFailureLocationObject(), alignment issues arise. Despite adding multiple objects to ol, only one image is displayed next to the text. How can I rectify this issue?

getFailureLocationObject() { const ol = [];

var o = {
  columns: [
    {
      image: "checked",
      height: 10,
      width: 10
    },
    {
      stack: [
        {
          columns: [
            {
              text: 'First column first line',
              width: '50%',
              margin: [5, 0, 0, 0],
            }
          ]
        }
      ],
      width: '*'
    }]
};

ol.push(o);
ol.push(o);

return o;

}

To see the issue and test the current setup, I have hard coded "First column first line", "First column Second line", "First column Third line". However, I aim for the method this.getFailureLocationObject() to iterate and create the list dynamically.

Experiment with it here!

https://stackblitz.com/edit/angular-pdfmake-example-3f14km?file=src%2Fapp%2Fapp.component.ts

Answer №1

Following some thorough testing with the provided code snippet, I have identified that the issue lies in the referencing of objects.

It appears that when pdfmake receives the values, it somehow mixes them up if they share the same reference (i.e., are the same object).

This deduction was made evident when modifications were made to the original code as shown below:

getFailureLocationObject() {
    const ol = [];

    var o = {
      columns: [
        {
          image: 'checked',
          height: 10,
          width: 10,
        },
        {
          stack: [
            {
              columns: [
                {
                  text: 'First column first line',
                  width: '50%',
                  margin: [5, 0, 0, 0],
                },
              ],
            },
          ],
          width: '*',
        },
      ],
    };

    ol.push(o);

    o = JSON.parse(JSON.stringify(o));

    ol.push(o);

    return ol;
}

With this adjustment, the error is no longer present. It should be noted that using JSON.parse(JSON.stringify(o)) effectively creates a brand new JSON object with identical content to o, ensuring that the objects within the array are distinct from each other.

This potential bug could be reported to the plugin's developers for resolution. In the meantime, employing clone methods to introduce unique objects into the array serves as a viable solution. For comprehensive insights on cloning objects and its significance, refer to this resource here.

If further clarification is required, please don't hesitate to inquire!

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

Error encountered with the Schema in expressjs and mongoose frameworks

I am currently working on integrating mongoDB with an expressjs project using mongoose, and I have encountered a specific error. throw new mongoose.Error.MissingSchemaError(name); ^ MissingSchemaError: Schema hasn't been registered for model ...

ArcGIS JavaScript API was unable to display weather information on the map

When I added the weather service layer along with the traffic layer, I encountered this error. The only difference is the URL. [esri.views.2d.layers.MapImageLayerView2D] TypeError: Cannot read properties of null (reading 'supportsDynamicLayers' ...

Guide on extracting a JavaScript string from a URL using Django

How can I extract "things" from the JavaScript URL "/people/things/" without any unnecessary characters? I've attempted inefficient methods like iteration, but struggle with removing the undesired parts of the string, leading to slow performance. Th ...

Using Angular to Apply a Custom Validation Condition on a FormGroup Nested Within Another FormGroup

I am facing an issue with my form validation logic. I have a set of checkboxes that need to be validated only when a specific value is selected from a dropdown. The current validator checks the checkboxes regardless of the dropdown value. Here's the c ...

Deactivate the second subradio button when the first option is selected and vice versa

Need some assistance, hoping for your help. I have 2 choices with subcategories. 1 - Option A ---- variant 1 ---- variant 2 ---- variant 3 2 - Option B ---- variant 4 ---- variant 5 ---- variant 6 Check out my code here Users must choose betwe ...

The error message "Cannot construct apickli.Apickli" is indicating a Type Error

Encountering this issue : TypeError: apickli.Apickli is not a constructor every time I attempt to execute Sendpostrequest.js again in the following step of a scenario. In A.js, the initial call to Sendpostrequest works without any problems. However, ...

Oops! Looks like there was an error. The text strings need to be displayed within a <Text> component in the BottomTabBar. This occurred at line 132 in the

My app includes a bottomTab Navigation feature, but I encountered an error: Error: Text strings must be rendered within a <Text> component. This error is located at: in BottomTabBar (at SceneView.tsx:132) in StaticContainer in StaticCont ...

javascript onload select the text in the textarea

Is there a way to have JavaScript automatically highlight the text inside a textarea when the page is loaded? ...

Error encountered in Angular 9: nativeElement.selectpicker is not a function while using bootstrap select

I am currently working on implementing a searchable select box with Bootstrap in my Angular 9 project. To achieve this, I have decided to use Bootstrap Select. Below is the code snippet showcasing how I have integrated it into my project: Within my templ ...

Transform the assurance into a JSON entity

Currently facing an issue with converting the promise returned by the service to the controller. My goal is to generate an array of JSON objects using the data within the promise. In the controller, this is what I'm receiving: https://i.stack.imgur.co ...

The angular2-redux-starter.git seems to be missing in action

While I suspect this may be due to pilot error, I am reaching out for assistance. If my approach is incorrect, please guide me on where I can improve. I am a beginner in the world of Angular. I have been attempting to launch my application through NPM. It ...

Utilizing GroupBy in RxJs for an Observable of Objects数组

I am working with entries of type Observable<Event[]>, and they are structured as follows: [{ "_id": 1, "_title": "Test Event 1", "_startDate": "2019-05-29T07:20:00.000Z", "_endDate": "2019-05-29T08:00:00.000Z", "_isAllDay": false }, ...

Regenerate main JavaScript files in Gulp whenever partials are edited

In my gulp task for javascript files, I included partial js files in the source and filtered them out to avoid building them unnecessarily. gulp.task("js", () => { return gulp .src([ src_js_folder + "*.js", src_js_f ...

Unlocking $refs with the Composition API in Vue3 - A step-by-step guide

I am currently exploring how to access $refs in Vue 3 using the Composition API. In my template, I have two child components and I specifically need to obtain a reference to one of them: <template> <comp-foo /> <comp-bar ref="ta ...

The click event for getelementbyid() function is malfunctioning

I need assistance with a website I am creating that plays audio when a certain condition is met. Specifically, I want the audio to play if x falls within a specific range of numbers, but also continue playing if x does not fall within that range after th ...

interactive form fields updating using javascript

Hey there! I'm a beginner in web development and currently facing a challenge. I have a form with 2 fields generated by one drop-down menu. I've managed to generate one field, but struggling to get the other due to my limited knowledge. I just ne ...

Leveraging File functionality in TypeScript

In the process of developing a web application with Angular 4 and Typescript, I encountered an issue while attempting to retrieve the date of a file for upload. Specifically, when trying to access the lastModified property of a File object, Typescript retu ...

connect the validation of forms to different components

I am currently working on components to facilitate the user addition process. Below is an example of my form component: createForm(): void { this.courseAddForm = this.formBuilder.group({ name: ['', [ Validators.required, ...

determining the dimensions of an SVG element following skew transformation using JavaScript

I have an SVG element called rect that has the following attributes: <rect x="50" y="10" width="20" height="30" style="stroke: #000000; fill:none;" /> After applying a transformation of skewX(10), the dimensions of the rectangle are altered. How c ...

JavaScript Birthday Verification: Regular Expression Pattern

I am currently testing out JavaScript form validation. The information provided is not being sent to any database, it's just for format testing purposes. All the regex checks are functioning correctly apart from birth. It seems like there may be an i ...