I am working with PHP and FPDF to generate a PDF invoice. The designers have created a couple of very helpful documents, there are three total. The first is a mockup of the invoice. The second shows fonts, font sizes, and font colors. The last document show the positioning of all elements on the invoice. My only problem, is that the document was made with pixels at a resolution of 300 DPI rather than in millimeters. Anyone that is familiar with FPDF will know that PDF’s created with FPDF can not be generated with pixels in mind. PDF documents in FPDF can only use point (pt), millimeteres (mm), centimeters (cm), or inches (in).So I needed to come up with a way to convert things to what I needed them to be. Here is the solution I came up with. First, we need to discuss the conversion factors. My document had a DPI of 300, DPI stands for Dots Per Inch. This means that in every in there are 300 pixels. But we aren’t using inches for measurements on the document, we are using millimeters. Well, there are 25.4 millimeters in an inch. So, how do we go all the way from pixels to millimeters. Simple.
In chemistry, we studied something called dimensional analysis. Dimensional analysis is a method for converting from one unit to another. So first off we multiply the number of pixels by 1 over our DPI (in this case 300 DPI) then we multiply that by 25.4 by one inch (number of millimeters in an inch) Once everything cancels out we are left with a fairly nice, simple and easy to use formula.
mm = (pixels * 25.4) / dpi
In order to convert back from millimeters to pixels we simply reverse the formula using simple algebra.
pixels = (mm * dpi) / 25.4
There you have it. A very simple way to convert from pixels to millimeters. And then from millimeters back to pixels.
So thats what they were talking about during my morning chemistry nap, very impressive. Heck, just the fact that you can remember terms like dimensional analysis is impressive. Now, see if you can use “dimensional analysis” to convert my wasted hours in chemistry class into future dollors lost.
Thanks
.
Very useful. Thanks.
Good article, thanks. I’m a developer and i’m developing a label creator where users can select an image add some text and create a pdf of labels to print, i’m using the fpdf library but after doing my image conversions from pixels for the image to be displayed on the website to mm’s to be displayed on the pdf.
This all works fine as the labels fit the right sizes the prob i’m having is that the image is showing a lot smaller on the web, and with users needing to set their overflow text this is proving a prob. Any advice on this? my conversions are correct as your formula gives me the same results as the conversion calcs i’ve used. /the dpi is set to 72, i dont know if that has anything to do with it as i have no knowledge of dpi’s
Thanks
Useful article, thanks.
Thanks, this is exactly what I was looking for. Very helpful.
But a pixel has NO measurement.