DavBfr / dart_pdf

Pdf creation module for dart/flutter
https://pub.dev/packages/pdf
Apache License 2.0
1.42k stars 633 forks source link

Extend NewPage with freeSpace #1648

Open 2S-Software opened 7 months ago

2S-Software commented 7 months ago

Use Case

I only want to execute a page break if the free space of the page is less than e.g. 50mm:

[
  pw.Text("First paragraph"),
  // NewPage only needed, if available space is less than 50mm
  pw.NewPage(),
  pw.Text("Second paragraph"),
]

Would it be possible to add a new parameter 'freeSpace' to the existing widget NewPage so that the page break is only executed if the free space is less than specified?

The default value of the new double parameter should be Null or Zero, so that in this case a page break is always generated as before.

Possible implementation idea

I had tried to derive my own widget class of NewPage, but the offsetStart and fullConstraints infos are missing. Perhaps the base class pw.Widget can be enhanced with a function 'newPageNeeded' (with the available space as a parameter). NewPage could then override this new function and would replace the following code in MultiPage.generate in a more generic way:

Existing:

// Create a new page if we don't already have one
if (context == null || child is NewPage) {

New:

if (context == null || child.newPageNeeded(freeSpace)) {
DavBfr commented 7 months ago

Looks good to me. Please provide an implementation PR.

2S-Software commented 7 months ago

Hi David Thank you for your positive response. I will gladly prepare a push request. However, this is the first time that I will be returning a PR via a fork. I may therefore need some support...

My first question is, where do I get the missing font files from so that all Golden tests work successfully? 9 of 166 tests are failing because following fonts are missing: hacen-tunisia.ttf, noto-sans.ttf, genyomintw.ttf and material.ttf. Unfortunately, I couldn't find it on the Google Font page.

Thanks for your support. Stefan

DavBfr commented 7 months ago

THe font files and other assets are downloaded using the makefile. usually you just run make on Linux. On macOS and Windows you might need to install that program.

Trung15010802 commented 3 months ago

any update?