jung-kurt / gofpdf

A PDF document generator with high level support for text, drawing and images
http://godoc.org/github.com/jung-kurt/gofpdf
MIT License
4.29k stars 772 forks source link

Is it possible to combine 2 gofpdf.fpdf documents? #325

Closed williamgorden closed 4 years ago

williamgorden commented 4 years ago

I am using gofpdf to output structural calculations that can sometimes be more than 100 pages Long. I used to write these documents by hand in a word processor with a little help from Excel files for the calculations. I now have a little console program written in GO that does this much better for me with gofpdf.

I always have to combine 2 fpdfs into 1 document with pdf-merge after they are finished. It is not possible to do this type of document in one loop from beginning to end because of the changing size of the first document while writing the second document. So I merge them. It is still much easier than the word processor.

However I am wondering about combining these 2 fpdfs directly in the program before writing them in just 1 file to disk.

I am seeing some issues here with gofpdi. Is it possible to do this with gofpdi?

Maybe an AddRange function or something like it so that the accumulated bookmarks in the second fpdf would also be added correctly to the bookmarks in the first fpdf.

I am not talking about reading 2 pdf files and combining them. I want to combine 2 fpdfs one after the other!

Is this possible?

jung-kurt commented 4 years ago

Any comments, @phpdave11?

williamgorden commented 4 years ago

I did actually look up the Adobe pdf spec to try and understand how pdfs are made up. I also looked through your code to see if I could understand it enough to at least visualize a solution. I am a bit overwhelmed by the whole thing. I can easily imagine that there are many things to keep track of in just adding another fpdf-document to the end of the initial fpdf-document.

I am also a thankful user of this package! It saves untold amounts of work.

If you have any ideas… that would be great.

I thought after reading the other issues about gofpdi listed here in gofpdf that converting a fpdf document to a template and then adding it in gofpdi would perhaps be the way of least resistance.

I am also calculating and writing the fpdfs to byte-arrays in a server and then sending them back to the client through gRPC where they are then written to disk. I travel a lot and only want to take a thin client with me to work on the road.

Maybe a function that combines 2 fpdfs in gofpdi is really the best solution.

Thanks again.

jung-kurt commented 4 years ago

I always have to combine 2 fpdfs into 1 document with pdf-merge after they are finished. It is not possible to do this type of document in one loop from beginning to end because of the changing size of the first document while writing the second document.

Is it possible to build up two dynamic, non-PDF data structures during the construction phase? They would contain enough information to generate the PDFs. After this stage, the combined PDF could be generated in a single pass with no special logic needed for bookmark and page number calculations.

williamgorden commented 4 years ago

The page numbers are the problem.

I need several lists in the first document that are based on the number of items of certain types in the second document: Illustrations, Calculations, Tables, Diagrams, etc. The lists need the page numbers where to find each item.

The first document is just the front matter with the general table of contents and all the lists.

This first document doesn't even get page numbers at all. It is a minimum of 2 pages long and can be 5 pages long or more.

I start numbering the pages in the second document.

Hold on tight… it gets worse! :-)

The results of the earlier calculations in the second document are used in the calculations later in the document. It is one huge loop of calculations passing the results along from beginning to end.

Thats why I just keep adding pages to the fpdf as I go through the calculations from the beginning to the end of the second document. Along the way I gather the lists of items for the front matter and write the first fpdf document at the end!

It works great!!! I end up with 2 documents: frontmatter.pdf and maincontent.pdf

I only have to merge the 2 documents at the end. I am NOT complaining about this small step. I am already saving myself lots of work with it just as it is.

However...

It sounds like my feature request is an unusual case and probably a lot of work to implement.

If I am the only one that wants it, then it can't become a priority.

I really appreciate you reading this, thankyou!

As a side note: A lot of english technical books are written this way. I just don't have the time to do it in LateX. Maybe this can be a future feature of godpdf.

jung-kurt commented 4 years ago

It may be that aliases could help you. Have you looked at the RegisterAlias example?

williamgorden commented 4 years ago

That looks very interesting!!!

Is it possible to create more than one table of contents like this in 1 document?

Each of my lists is actually a table of contents.

jung-kurt commented 4 years ago

Is it possible to create more than one table of contents like this in 1 document?

Certainly! You just need to come up with a unique text key that will be replaced, when the document is rendered, with its corresponding text value.

williamgorden commented 4 years ago

In the examples I see {mark %d}. Can I use {content %d} for contents and {diagram %d} for diagrams to keep the lists separate??

jung-kurt commented 4 years ago

Yes. You should use any text that will be recognized by the search-and-replace operation that takes place when the PDF is actually written. The UTF-8 version of the example shows different alias text. Even the curly braces are not necessary -- you just have to choose keys that won't otherwise occur in your text. (Otherwise some unexpected replacements would be made.) I hope this scheme works for you.

williamgorden commented 4 years ago

I need to study the examples closely!!

Thankyou very much!!

There is something similar to this in LateX.

I just need to be careful about it.

We probably can close this issue as solved.

Or we can leave it for someone who might need it.

Have a nice evening!