go-weasyprint icon indicating copy to clipboard operation
go-weasyprint copied to clipboard

Use case : point of sale ticket printing (convert html to image with size detection)

Open maitredede opened this issue 9 months ago • 2 comments

Hello,

I have a production use case. I have developped a point of sale application. First time with java api, rewriting in golang. The principle is to generate point of sale tickets as HTML (easy layout, style, images, fonts...), and then convert it as image to send to printer.

So far, I did it with following methods :

The best results so far are obtained with wkhtmltoimage (uses a headless browser, but slow since no acceleration) and html2canvas (fast, but requires a desktop environment, with hardware rendering acceleration).

I would like to use a pure golang solution, like go-weasyprint or webrender, but I have not found a simple example of each library. So far, I have managed to use go-weasyprint, to generate PDF, then use poppler for PDF to PNG conversion, but I think I will hit a problem : tickets are one page with variable size. The width is known and fixed, but the height is unknown and vary with ticket contents.

With wkhtml2image, height was automatically detected, and resulting image was sized correctly, but using weasyprint/poppler, I have not managed to have dynamic page size.

Is there a chance that using your libraries (either go-weasyprint or webrender) that I can convert html to image with size detection ?

Thanks.

maitredede avatar Feb 12 '25 02:02 maitredede

Hi,

Thank you for this use case. I see two subtasks you want to handle : 1) knowing the resulting page size 2) concert to an image

I'm afraid goweasyprint can't help with converting to images. It would require using a rasterizing library, which is out of scope for this project.

Point 1) however, should be doable, using the PageBox. I'll add a snippet when I'm back I'm front of my computer

benoitkugler avatar Feb 12 '25 08:02 benoitkugler

Hi, thanks for your consideration.

The first point you mentionned is the key. In my case, I know the device printable area width (512pt for printers I use), so html elements can fit this whole width. But the available height is almose infinite (paper length), so I need to know the height required by html elements to fit in single page. For a generic use, it may be interesting to have automatic size detection, with or without maximum bounds. In my case, that means that elements can fill a 512pt (or px) width area, and take all required height it needs.

For rasterization, I will try to implement it with https://github.com/tdewolff/canvas or https://github.com/tfriedel6/canvas. So far, your backend interface does not look 1-1 compatible, I will try to figure out.

maitredede avatar Feb 13 '25 01:02 maitredede