Urgent.News

What's breaking now, across hundreds of outlets.

Tech

Paginating a long receipt at its print size

On a phone, you can scroll through a receipt image that's 3,500 pixels tall. At Repceipt's print scale, that's nearly 44 cm of paper. Shrinking it to fit on a single Letter sheet makes the prices harder to read. You can see how small receipt type gets in Repceipt's receipt typography study . The home-print export puts that receipt on two sheets. It starts with the PNG rendered by the server and…

When viewing a receipt on a phone, the image can be quite tall, stretching to around 44 centimeters on paper at its intended print size. However, when Repceipt attempts to print this receipt on a single Letter sheet, the font size becomes impractically small. To accommodate the entire receipt, Repceipt divides it across two printed sheets.

The process begins by rendering the receipt as a PNG image on the server. Repceipt then calculates how many rows of pixels can fit on each page at the desired print size. It uses a conversion factor of 8 pixels per millimeter to determine the physical dimensions of each slice of the receipt image. For a receipt that is 576 pixels wide, this results in a width of 72 millimeters on paper.

To convert pixels to PDF points, Repceipt divides the pixel width by 8 to get millimeters, then converts those millimeters to inches by dividing by 25.4, and finally multiplies by 72 to obtain PDF points. A PDF point is defined as 1/72 of an inch at the default scale. With a Letter page height of 792 points, accounting for 0.5-inch margins at the top and bottom, leaves 720 points of space for the image.

Dividing this space by the size of one pixel in points yields the number of rows that can fit on each page. For a 576-pixel-wide receipt, this equates to 2,032 rows on the first page and 1,468 on the second.

The `slicePngSource` helper then creates a temporary canvas for each full-width slice of the receipt, copying it to a new canvas with the same pixel dimensions. Each slice is embedded as a separate PNG image using pdf-lib, the PDF library employed by Repceipt. PDF coordinates start at the bottom of the page and increase upward, necessitating adjustments to position each slice correctly within its assigned page.

To center the slices horizontally, half of the leftover page width is used as the horizontal offset. The code iterates through the receipt image in chunks, determined by `rowsPerPage`, to generate individual PNG slices. Each slice is placed on its respective page using PDF drawing functions, ensuring that even the smallest final slice is accommodated within the margins of the second page.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

How to fix link previews that don't show up (or show the wrong image) on Slack, X, LinkedIn, Discord and WhatsApp

Scheduled broken-link and redirect checks that tell you the moment a link breaks — linkbruno.com You publish a post, paste the link into Slack or LinkedIn, and instead of a nice card with a title and…

  • Broken link previews often result from issues with Open Graph and Twitter Card tags.
  • Ensure tags are in server-rendered or pre-rendered HTML, use absolute URLs for images.
  • Adjust user agents, change image URLs, or use crawlers to resolve platform-specific issues.

Moving your blog from Medium (or Substack, or Hashnode) to your own domain? An 11-point checklist so the links survive

Scheduled broken-link and redirect checks that tell you the moment a link breaks — linkbruno.com Moving a blog between two sites you control is a redirect exercise: map old URLs to new ones, add 301s…

  • Confirm custom domain control for straightforward redirecting
  • Create URL inventory of all posts, titles, and publish dates
  • Identify external links to maintain control over them

From Vision to Reality: Building an AWS Student Community at My College

A few months ago, I wrote about being selected as an AWS Student Builder Group Leader (SBGL) and what I hoped to build at Tribhuvan University, Bhaktapur Multiple Campus.

  • Author led AWS Student Builder Group at Tribhuvan University
  • Faced challenges in organizing community, learned on the go
  • Organized first event "Your First Step into AWS & Cloud Computing"

More from Monday 23 November →