-1

There are many libraries to create pdf files with php : fpdf, tcpdf, domPdf, ...

There is also a php extension PDFlib, but it's not free.

My quetion is how to create a simple pdf file using only native php ?

khairul alam
  • 138
  • 13
  • 4
    You reverse engineer the structure of PDF files and make a lot of use of `pack()` function. Jokes aside, you need a lib, PHP doesn't do this natively. In theory you can do it, but there is no reason to do it yourself. (I'm not the downvoter) – Daniel W. Dec 14 '21 at 15:24
  • @DanielW., i don't see any reason to "not do it yourself" ! Beside we can do many things with php (creating images as example) – Lagrida Yassine Dec 14 '21 at 15:27
  • 3
    Why would you want to, when these libraries already help you to do it? If your intention is to create a better library, or just for an academic exercise, then expect to spend quite a while studying the PDF specification and working out how you construct commands which would translate some given input into a corresponding, correct portion of a PDF document. PDF is a complex, unpleasant format, so I hope you've got plenty of time on your hands... – ADyson Dec 14 '21 at 15:28
  • 2
    Most people don't create images with PHP natively, they use the GD lib or ImageMagick lib. PDF is not like HTML where you nest a few containers and it looks good. You need to understand the structure and pack the data into binary sequences. – Daniel W. Dec 14 '21 at 15:28
  • I have my reasons, fpdf for example use an old php version, beside domPdf for example have about 60Mb of size, but i need only creating a simple pdf file with one image. Pdflib is not free .. – Lagrida Yassine Dec 14 '21 at 15:31
  • @DanielW. i knew that php use extensions for some features (GD, Pdflib, ....) – Lagrida Yassine Dec 14 '21 at 15:34
  • You could use a tool that doesn't depend on PHP but is callable from PHP, like one of the dozens [`html2pdf`](https://unix.stackexchange.com/questions/533886/is-there-a-command-line-tool-for-converting-html-files-to-pdf) tools. – Daniel W. Dec 14 '21 at 15:37
  • I prefer using a php function, but if i don't find a method (not exists maybe) i will think on other tools. I didn't imagine that creating pdf files with php can be that hard .. – Lagrida Yassine Dec 14 '21 at 15:44
  • 1
    Creating PDF files in any language is hard, because PDF is hard. Nothing to do with PHP specifically. One of the reasons there are lots of competing libraries and other tools is because none of them are perfect, or have all the features. – ADyson Dec 14 '21 at 15:53
  • 1
    `60Mb of size`...is trivial in a modern server. Not worth worrying about, especially not compared to the immense effort of re-implementing something anywhere near equivalent. If it was still 1995 I might have been worried about that amount of storage being used by one component, but I wouldn't even give it a second glance nowadays. – ADyson Dec 14 '21 at 15:55
  • @ADyson, i see, but what i ask is simple pdf file (with text, and image), not the whole pdf features (sections, titles, numerations, ...) – Lagrida Yassine Dec 14 '21 at 15:57
  • http://www.fpdf.org/ states _"The latest version requires at least PHP 5.1 and is compatible with PHP 7 and PHP 8"_ ...so I don't know how you can state that fpdf "uses an old PHP version"...that's simply not true. PHP 8 is the newest version of PHP. Make sure you do your research (and that wasn't difficult research!) before making any claims. – ADyson Dec 14 '21 at 15:57
  • 1
    `what i ask is simple pdf file`...so then you can just use the features of the library which you need, and ignore the rest, and stop worrying. You probably don't use all the features of your operating system, or your washing machine, or even your car, either, but you would not start trying to build your own version of those (highly complex and specialised) devices just because the machine has more buttons than you really needed. And anyway even "simple" PDF documents are difficult to create, because even the core of the specification is complex. Go and read it, if you don't believe us. – ADyson Dec 14 '21 at 15:59
  • @ADyson, Ok thanks, i will try to do it (With a great motive of curiosity :) ) but FPDF didn't work with my php 7.4, i get many errors, the first of them : `Deprecated: Function get_magic_quotes_runtime() is deprecated ...` – Lagrida Yassine Dec 14 '21 at 16:14
  • That's a _notice_, not an error. It warns you that this feature will stop working _in a future version of PHP_. If you pay attention when you run that code, you'll see that it will still do whatever it was going to, regardless of the notice being emitted. You can configure PHP to send all such errors, warnings and notices to a file when you're running the application live, to ensure they don't get tangled up with the real output. – ADyson Dec 14 '21 at 16:17
  • And `get_magic_quotes_runtime()` is a general PHP function, it's not directly part of fpdf. fpdf is presumably making use of that, but you can be sure they'll tidy it up when they release a version of fpdf which is designed for the version of PHP in which `get_magic_quotes_runtime` is finally removed. In fact, as per the [manual page](https://www.php.net/manual/en/function.get-magic-quotes-runtime.php), it's already been removed in PHP8, so I'd expect that the version of fpdf released for PHP 8 will have stopped using that function (because if not, users of fpdf will be complaining a lot!). – ADyson Dec 14 '21 at 16:19
  • In fact there is a `Fatal error` behind the problem, and not the warning : `Fatal error: Uncaught Exception: FPDF error: Some data has already been output, can't send PDF file ` – Lagrida Yassine Dec 14 '21 at 16:22
  • Well that's likely because your PHP script has already accidentally output some content prior to FPDF being invoked. This error has nothing to do with the deprecation notice. – ADyson Dec 14 '21 at 16:23
  • Read https://stackoverflow.com/questions/9475686/fpdf-error-some-data-has-already-been-output-cant-send-pdf (you could have googled the error already and found this explanation yourself - again...do your research properly.) – ADyson Dec 14 '21 at 16:24
  • Finally i get my PDF, i see the question before but all tries are failled for me except for one, i will keep searching on pdf structure and working with fpdf at the moment (i should see how fpdf create pdfs first :) ) – Lagrida Yassine Dec 14 '21 at 16:31
  • Thank you @KJ for the contribution, I would be grateful if you could post an answer with more details – Lagrida Yassine Dec 14 '21 at 18:33

1 Answers1

2

I provided a link to how a PDF might be build from scratch using basic text structures as discussed in https://stackoverflow.com/a/68442444/10802527 however like in the discussion for another limited Arduino solution https://stackoverflow.com/a/68876321/10802527 the topic usually leads to how to handle images.

I am going to suggest the simplest of all 3rd party library methods that works across platforms is Artifex Mutool so to build a simple page via scripting we simply need to use text building blocks like this.

%%MediaBox 0 0 595 842
%%Font Helv Helvetica Latin
%%Image Flowers1 C:/Users/name/Documents/WelcomeScan.jpg

% Draw an image. x width, H line elevation (y skew), x skew, y height, left offset, bottom offset, units are pt.'s cm is not centimetres
q 512 0.0 0.0 384 41.5 400 cm /Flowers1 Do Q

% Draw a rectangle. move line fill
q 1 0.5 1 rg 41.5 370 m 553.5 370 l 553.5 270 l 41.5 270 l f Q

% Show some text.
q 0 0 1 rg
BT /Helv 24 Tf 210 330 Td (Hello, Flowers!) Tj ET
BT /Helv 24 Tf 100 290 Td (Microsoft Windows Welcome Scan) Tj ET
Q

This was the code to produce

mutool create -o "output.pdf" -O ascii "Page1.txt" ["page2.txt" ...]

enter image description here

K J
  • 8,045
  • 3
  • 14
  • 36