Automation: Extracting waybill label from DHL PDF without screwing up barcodes. - Page 1
Forum section: electronics / general

author: Psi
author_rank: Super Contributor
timestamp: May 18, 2025, 06:49:00 am
content: Anyone have a production method to get an unscaled/un-antialiasing image out of a PDF file?I'm trying to automate printing of DHL shipping labels. They come as an image intended for a 100x170mm label but in the middle of an A4 page in a multipage PDF document.Adobe reader will display and print the barcode correctly, but I've tried extracting an image at 600dpi for the label printer with various command line tools like Ghostscript and Poppler(pdftoppm) and I can't get an image output where the barcode lines have the intended width/gaps.Obviously there is some conversion/antialiasing happening and resize rounding that's causing the edges to get thicker/thinner.Anyone know how to get an image out of a PDF in raw/native form so the barcodes are all correct?I don't want to buy full adobe licensing. So the JavaScript control API is out.I even tired adobe reader activeX but it's very limited and doesn't let you print page snapshots.Also, Does anyone know what waybill label output you get when you use the DHL API to programmatically generate shipping docs instead of using the website? (That is my next project. so i'm wondering if maybe that actually solve this issue.)

author: loki42
author_rank: Frequent Contributor
timestamp: May 18, 2025, 10:55:46 pm
content: We just generate them as ZPL using the API and send that to the printer. They also have an option to print thermal panels from their website and generate pdfs without the extras.

author: Nominal Animal
author_rank: Super Contributor
timestamp: May 18, 2025, 11:54:36 pm
content: Quote from: Psi on May 18, 2025, 06:49:00 amIf it is a separate image, thenpdfimagesfrom e.g.poppler-utilswill extract it for you.I personally would probably usepdfseparatefrompoppler-utilsto extract the target page, then usepdfcrop(from e.g.texlive-extra-utilspackage, it's a common Perl script) to crop the single-page to just the label area.To provide a shell script, I'd need to see an example one (via email or PM); the one extracted page (via say "pdfseparate -fPAGE-lPAGEsource.pdfonepage.pdf") would suffice. In all cases, the trick is to use vector formats –– PDF, PostScript, SVG –– instead of rasterizing it. As the labels are vector graphics (either via barcode font or generated vector graphics), they will be filled but not stroked polygons. It is the extra stroking that makes the inked areas too large; it's a common issue in PDF rasterizers. SVG in particular can represent such vector images exactly, and even keep the physical dimensions/size accurate. Withpdimages, you'd get the original image; withpdfseparate+pdfcrop, a PDF image with just the 100mm × 70mm label in it.(Many applications fail to retain correct physical size with SVG, simply because they don't understand thatviewBox="Left Top Right Bottom"attribute defines in user (unitless) units the equivalent of the crop box in PDF, and thatwidth="W" height="H"attributes define the physical size of that box (so you need to include the physical units here, for examplept,in, ormminWandH). This is not any fault of SVG, just the stupidity and incorrect assumptions of application developers showing. For example,<svg viewBox="0 0 4015.748 2362.205" width="170mm" height="100mm" ...>...</svg>defines a 170mm×100mm image with user units as 600 DPI ≃ 23.62205 units/mm, so that<rect x="1200" y="300" width="600" height="600" fill="#000000" stroke="none"/>generates an exactly 1" by 1" filled square one two inches from the left edge, and half an inch from the top edge.)To implement something like this as a shell script for batch processing, I'd recommend using an autodeleted temporary work directory,#!/bin/bashWork="$(mktemp -d)" || exit 1trap "cd /tmp ; rm -rf '$Work'" EXITso that you can extract temporary files to"$Work/", and they will be automatically deleted when the script exits. Note that the trap event string quote order is quite important: the outer double quotes means the value of$Workis expanded when the event is set, so even if you do sayWork="/"orWork="$HOME"later on, the cleanup exit event is not affected, it will still only clean up the original temporary directory.

author: Psi
author_rank: Super Contributor
timestamp: May 20, 2025, 08:58:05 am
content: Quote from: Nominal Animal on May 18, 2025, 11:54:36 pmsadly no, it's not just an image in the pdfQuote from: Nominal Animal on May 18, 2025, 11:54:36 pmThanks, will give it a try working in vector formats.That does sound like a better plan

author: 48X24X48X
author_rank: Frequent Contributor
timestamp: May 20, 2025, 12:53:40 pm
content: I used krop for this same purpose. Runs on Linux. It just knows where to trim exactly.

author: Gribo
author_rank: Frequent Contributor
timestamp: May 20, 2025, 05:33:05 pm
content: Zxing - If your only option is a barcode image, Zxing might be able to decode it. Make sure you capture the barcode and only the barcode. It would also help to know what format the barcode is (Code-39, code-128 etc.).Edit: Since you mentioned ActiveX, there is a .NET port of ZXing too.

author: thm_w
author_rank: Super Contributor
timestamp: May 20, 2025, 08:49:03 pm
content: If you are using DHLs website there is an option to produce a 4x6 label, as mentioned above. You also get the same choice if you go through a third party that uses their API. Any half decent shipping platform would have the option.Unless you are high value ($3k+?) then its paperless and you only need the label, nothing else.

author: NorthGuy
author_rank: Super Contributor
timestamp: May 20, 2025, 11:19:56 pm
content: Quote from: Psi on May 18, 2025, 06:49:00 amI just made sure the barcodes are scaled the same as they were in the original label. The original label was 4-inch wide already. So, I just moved it horizontally to the correct place. The vertical dimension was more than 6-inch. Instead of shrinking it, I removed the white space between barcodes to make it fit. Took me two hours and lots of wasted paper to get it right. This works with DHL without problems, for about 8 years already, without modifications.