Dev.to WebDev 🛠 Dev 👁 0 📖 2 min read

Why a Phone Photo Turns Sideways in a PDF—and Where to Fix It

A phone photo can look upright in a viewer and still arrive sideways in a generated PDF. The useful debugging question is not “Is this file portrait?” but “At which stage did the visible orientation first become wrong?”

A phone photo can look upright in a viewer and still arrive sideways in a generated PDF. The useful debugging question is not “Is this file portrait?” but “At which stage did the visible orientation first become wrong?” Keep the original file and trace the image through the pipeline before changing rotation logic.

A photo file can contain an encoded pixel matrix plus orientation metadata. A viewer may interpret that metadata and display the image upright without changing the stored pixel matrix. A decoder may instead produce working pixels that already follow the metadata. Later, an application may apply a user-selected rotation, and the PDF exporter may place the resulting image on a page. Those are separate decisions.

The filename and the apparent width and height alone do not establish which orientation the user should see. A CSS rotation that makes a preview look right also does not prove that the pixels sent to the exporter are upright.

Find the first incorrect stage

Use a copy of the original image that demonstrates the issue. First, open it in a preview known to interpret orientation metadata and note the intended upright appearance. Then inspect the decoded working image or canvas before any PDF placement and before adding a manual correction. Finally, reopen the saved PDF, preferably in another viewer, and compare it with the earlier stages.

Consider an illustrative case: a photo looks upright in the original preview, sideways in the decoded canvas and sideways in the PDF. Investigate decoding first. If the canvas is upright but the saved PDF is sideways, inspect image embedding, page placement and export handling. If both are correct until a user rotation is applied, trace how that choice is stored and applied. These are diagnostic branches, not claims about a tested file.

Give orientation one owner

The MDN documentation for createImageBitmap describes its default from-image behavior as following orientation metadata when present. It also describes none as ignoring that metadata, while flipY follows the metadata and then flips vertically. Check the options and fallback path your application actually uses; documentation alone does not establish behavior across every browser.

Normalize orientation once at the decode boundary your application owns, and document what the next stage receives. Keep the user’s requested rotation distinct from that normalization. Do not add a blanket 90-degree correction for portrait files, or a CSS transform solely because one preview appears sideways: either can apply a correction twice or leave exported pixels unchanged.

If the working bitmap is wrong, inspect decoder options and browser fallbacks. If it is right but the PDF is wrong, follow the bitmap into layout and export before altering decoding. After a change, use an actual saved PDF and an independent viewer to check the behavior you need; a convincing in-browser preview is only one stage of evidence.

Disclosure: GPT-6.1 Terra drafted this article for the LensUp operator. An independent AI editor polished it on 15 November 2026, and Codex, an AI agent, checked its factual statements against documentation and a static source reading. No new browser, image-output or PDF test was performed.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.