Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I propose to add a builder #2

Open
pleshevskiy opened this issue Oct 25, 2021 · 1 comment
Open

I propose to add a builder #2

pleshevskiy opened this issue Oct 25, 2021 · 1 comment

Comments

@pleshevskiy
Copy link

It seems to me that the builder design pattern is most suitable for this library.

const brandInvoiceBuilder = new InvoiceBuilder()
   .useStyles(
       new StyleBulder()
           .withDocument({ marginLeft: 30, marginRight: 30, marginTop: 30 })
           .withFonts({ ... })
           .withHeader({ ... })
       // or use style objects as well
       // if you pass style builder, invoice builder will build your styles automatically
   );

// then in any business logic
brandInvoiceBuilder.generate(invoiceData, 'path/to/your/invoice.pdf');

If you don't use custom styles, it will generate an invoice with default styles. Also if you use StyleBuilder with no or partial use of its methods, it will create default styles or your styles that have been merged with the default styles

const defaultInvoiceBuilder = new InvoiceBuilder();
// or new InvoiceBuilder().useStyles(new StyleBuilder())
defaultInvoiceBuilder.generate(invoiceData, 'path/to/your/invoice.pdf'); 
@pleshevskiy
Copy link
Author

pleshevskiy commented Oct 25, 2021

Alternatively, we could put StyleBuilder to above level. And also add a Prototype (Clone) design pattern. This makes it possible to generate styles for each brand more conveniently

const generalInvoiceBuilder = new InvoiceBuilder()
    .withDocument({ marginLeft: 30, marginRight: 30, marginTop: 30 })
    .withFonts({ ... })
    .withText({ ... })

const fooBrandInvoiceBuilder = generalInvoiceBuilder.clone()
    .withHeader({ ... })
    .withTable({ ... })
// fooBrandInvoiceBuilder.generate(data, 'invoice.pdf');

const barBrandInvoiceBuilder = generalInvoiceBuilder.clone()
    .withText({ ... }) // will be merged with general text styles
    .withHeader({ ... });
    // used default styles for `table`
// barBrandInvoiceBuilder.generate(data, 'invoice.pdf');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant