3 Stories About Generating PDF Documents
Published: 2022-01-04 09:04:43


I want to share my experience of building PDF generators. 

PDF export from HTML pages was always a big problem. A few times we were stuck for weeks while building food tech projects. Being aware that it might cause a problem, I decided to separate our functionality and be able to play with pdf without caring about everything else.

**First story**

The first time where I was involved in PDF was an old PHP5 project my company was assigned to our team to rebuild everything from scratch and reduce 10 years of tech debt.

This project was generated reports based on static data, that was acquired from business owners and top managers.  It was a survey, where top-level managers can tell about problems they face. The questionnaire was built that way to provoke to reveal the truth, even in complex situations. After that, information gets analyzed and returned with advice on how this can be solved. With a list of steps, that the company should do and overgrow their problems.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g3wpxg0f38xkfwo5isqs.png)
 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kqilsue3n1lmpzj9v328.png)
 



Later that data was merged and stored in a database pool of data combined by the same industry. And after processing the survey you can review industry reports and make long-term predictions.

The first version of that report export was created in 2005. At that time, web-based software was simple and has very limited functionality (weak).

Previous reports were saved in the database as files(sic!). When I received all this data and files with results combined weight was about 1 Gb. That was a huge amount of valuable data at that time. Just try to remember what the internet and phones were like in 2005. ;)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dobxl515gse8ywixujl1.png)
 
So I know a lot about problems with PDF files. And this article might help you if you want to  generate custom invoices, reports, meal plans, and food information, like recipes.

When I realized that we had a huge demand for building PDF export tool, I was ready to jump into it.

Before starting to code, I prepared a list of libraries/npm packages that doing PDF documents. I’m thankful to God for those tools from this list. They are evolving and becoming better and better.

I tried different packages, some of them I just reviewed. A lot of them sounds promising but didn't meet our needs.

I picked [React-PDF](https://github.com/diegomura/react-pdf). [The creator](https://github.com/diegomura) of this package uses an interesting approach, has a good community, and heavily building it. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yzaefw6nxw8c6pe30r2k.png)
 



[https://react-pdf.org/](https://react-pdf.org/)
You can read more about ReactPDF v2 here: [https://react-pdf.org/blog/announcing-react-pdf-v2](https://react-pdf.org/blog/announcing-react-pdf-v2)

Dev community that uses this package provides some financial support. I'll put a link here - if you want to become a supporter - It'll be awesome.

[Open Collective maintains all finances behind](https://opencollective.com/react-pdf). If this article will help to raise additional support = I will be happy. Because it's a good open-sourced library.

[link to GitHub of my source](https://github.com/GroceriStar/pdf-export-component)

**Second story**

At one of the food tech startups all data was stored in WordPress-database and used custom plugin for displaying recipes and ingredients.

Plugin didnt provide all functionality that we want to give to our users. Our goal was to extend the calculations, based on user preferences. And we dont have any real opportunity (not joking) to make calculations on backend side. So we need to joggle with Javascript before we display data at frontend side.

To make it work as we need to do strange moves: we must generate an HTML page, using pure data from the database. 

Then we start our DOM manipulations:
* we hide the main content
* make calculations
* remove some data
* merge some "table cells". 

And only after these manipulations, we were able to finish this HTML page and send it to print from browser method.

**Third story**

At the next food project, my partner was responsible for coding PDF export. He is an experienced developer but was going crazy while coding this functionality. During the debugging process he display strings like 'hello', '123'... 

He was swearing a lot because let's face it - simple things can generate a lot of trouble.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x481zcpd0fnj2ow6fc2h.gif)
 


He was stuck for a few days with displaying images in PDF documents. Images can be uploaded in different sizes, different modes: landscape or portrait; and when you display a meal image -> it cannot be shrunk, because it will make a meal looks ugly.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/604j5wexar1mb4fyhdjm.jpg)
 
At some point, he sent me an exported document. Inside this document, I saw a picture of an old man with flowers on his head. It was a sign that he completed this part of a project :)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r4jig6vqknv571q8bq3f.png)
 
So when someone is working or testing PDF reports -> you can see that he has a lot of PDF docs, downloaded from the web project.

So we were building our module, highly connected to react-pdf. It's far away from ideal. But it worked and could be used by other developers and save time. If you like what we do -> feel free to star our repository on Github[https://github.com/GroceriStar/react-print-pdf].

As proof we're launching this repository, to show how it works and looks. [https://github.com/GroceriStar/react-print-pdf](https://github.com/GroceriStar/react-print-pdf)

Not sure when it will be ready, but can be a starting point.
Styles looks ugly right now, but it's easy to enable styles. My main goal was to be able to adjust things as necessary.

Screenshots:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/intgjq68tpnriz5v7pnt.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/slhqhlglpt1hjdob34i3.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w8qzo3nr09gq59vt0wcs.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a5e6q972g8md003rcmi9.png)