1

I'm using the pdfmake lib to construct a pdf in Javascript. I receive json data of the layout of the page and the text/images that need to be displayed. My issue is that for pdf to recognize that two or more text/images are next to each other on the page you have to construct the content like this:

content: [
    {
        alignment: 'justify',
        columns: [
            {
                text: 'Lorem ipsum dolor ..'
            },
            {
                text: 'Lorem ipsum dolor ..'
            }
        ]
    }]

For reference, see this link. (under Columns)

So in other words, I need to filter my layout for items that have the same y value, and construct a column with the text/image for each of these. I already have working code that uses the data I receive and constructs content, but can't seem to figure out how to filter the data for items that are next to each other.

The data I receive looks like this:

[
 {
  height: 8,
  width: 12,
  x: 0.
  y: 1,
  type: 'text',
  text: 'Testing',
  textType: 'heading1'
 },
{
  height: 6,
  width: 12,
  x: 0.
  y: 2,
  type: 'text',
  text: 'Testing_again',
  textType: 'paragraph'
 },
{
  height: 6,
  width: 12,
  x: 0.
  y: 2,
  type: 'text',
  text: 'Testing_once_again',
  textType: 'paragraph'
 }
]

So in the above example, my finished content should look like this:

content: [
    {
        text: 'Testing'
    },
    {
        alignment: 'justify',
        columns: [
            {
                text: 'Testing_again'
            },
            {
                text: 'Testing_once_again'
            }
        ]
    }]

In other words, all objects that have the same y value should be put in columns. Any help in accomplishing this would be greatly appreciated.

Note: This is my first post, so please let me know if I need to add something or explain anything in a better way. TIA

Arrie
  • 11
  • 2
  • Sounds like you'll want to group the objects by `y` value, see the [linked question](https://stackoverflow.com/questions/14446511/most-efficient-method-to-groupby-on-an-array-of-objects)'s answers for various ways to do that. Happy coding! – T.J. Crowder Feb 24 '20 at 09:06
  • (BTW: As first questions on SO go, this is *pretty darned good*, nice one! One thing that people would probably be looking for that isn't in it is your attempt to solve the problem, even if it doesn't work [if it did work, you probably wouldn't be posting the question!] or if it does, but you think you've gone completely the wrong way with it. But again, compared to other first questions here, this is miles ahead and really shows an effort to be clear and complete.) – T.J. Crowder Feb 24 '20 at 09:09
  • @T.J.Crowder, thanks! Sorry about that, was thinking of adding my code as I mentioned it in the question, I'll definitely add it next time. Thanks for the linked questions, I found a working solution – Arrie Feb 24 '20 at 09:20
  • That's great! Happy coding! :-) – T.J. Crowder Feb 24 '20 at 09:21

0 Answers0