Splitting a large PDF file with PHP (using a text marker as the place to split)

I have a website I made myself that I use at work. We do delivery routes, and I have what we call “directionals” on this site. These are just a list of directions, instructions and customer addresses and the products they get, etc. Currently I have to file > print (print to pdf) for each delivery route I have (I have 43). I know I can write code that will load all of my directionals on one page and then File > Print (Print To PDF) from there. However, this would not be a very good solution for us, as the PDF file would be 600 pages or so and we would be swiping the tablet screens for 20 minutes to get to the directional we actually want to use. We use tablets in our vehicles to display the PDFs.

Here’s what I want to do -

I want to print out all of my route directionals as one PDF file[as mentioned above where I said it wasn’t a good solution] (i’ve seen other solutions, but simply put, they are far beyond my knowledge and scope) and at the start of each directional in that master PDF file, I would put some sort of text marker (something like ^ - a character never used in our directions). Then I would like to something along the lines of

$pdfIndividualContent = explode(’^’, $pdfContents);

Then proceed to save all the chunked files by their relevant route names into a directory. That way instead of waiting AN HOUR to print all PDF files for individual routes, I could spent 10 minutes waiting on the print dialog to finally load hundreds of pages, and then save the PDF - process the PDF with PHP code, and then drag an entire folder over to my company tablets. This would allow us to be able to have updated customer lists at all times, and quickly. We have strict delivery deadlines daily, and don’t have a lot of time to sit at a computer and try and print 43 sets of instructions for routes.

I’ve been working on this site for 2 years, and due to the nature and confidentiality of the content, I cannot share it, nor is it on a public server.

Any help or ideas is greatly appreciated.

Your problem seems to be that you have output that is currently being (can be) displayed on a web page, that you would like to automatically save as individual .pdf files?

If so, you would use a php library like TCPDF https://tcpdf.org/ and instead of outputting the html for each individual route onto a web page, you would supply it to the tcpdf ->writeHTML() method, then output each resultant .pdf document to an appropriately named .pdf file.

Something like TCPDF will definitely work but you’ll need to construct each PDF manually (eg. create a blank PDF, create a text box, flow the text in, create another text box, etc.).

If you already have the HTML pages I would suggest using WKHTMLtoPDF instead. It’s a command line executable that you point at an HTML page which outputs a PDF, as if you had printed it. Run a batch to output each one and you’re done.