HTML to PDF Displays EXIF Oriented Images Incorrectly

Product: Aspose.PDF for .NET
Version: 25.8.0
Runtime: .NET 8.0
OS: Windows 11 Enterprise 24H2

Summary
Images that rely on EXIF Orientation for upright display appear rotated in the PDF when converted from HTML. In the browser the same HTML shows the image upright. The JPEG raster is landscape and the file uses EXIF Orientation=6 to indicate portrait. The PDF shows the image on its side, which suggests EXIF is not honored during PDF creation.

Attachments
I attached a ZIP with a minimal PoC and the resulting output.pdf. The ZIP includes:

  • image_exif_orientation_6.jpg JPEG with EXIF Orientation tag 274 = 6. Raster size 3024x4032
  • index.html Minimal HTML with a single <img> referencing the JPEG.
  • AsposeExifRotationPoC.csproj and Program.cs .NET console app that loads the HTML and saves a PDF.
  • run.ps1 and run.sh Optional helpers for build and run.

Additionally I have included the output.pdf file produce by running the console application.

output.pdf (3.5 MB)
aspose-exif-rotation-poc.zip (3.3 MB)

Steps to reproduce

  1. Open index.html in a modern browser. Image appears upright portrait.
  2. Run the console app (or skip to step 3 using the attached pdf).
  3. Open output.pdf. The image appears rotated 90 degrees relative to the HTML view.

Expected
HTML to PDF honors EXIF Orientation and the image appears upright portrait, matching the browser.

Actual
Image appears rotated 90 degrees in the PDF.

Minimal code used

using System;
using System.IO;
using Aspose.Pdf;

class Program
{
    static int Main()
    {
        string baseDir = AppContext.BaseDirectory;
        string htmlPath = Path.Combine(baseDir, "index.html");
        var options = new HtmlLoadOptions(baseDir);
        using var doc = new Document(htmlPath, options);
        doc.Save(Path.Combine(baseDir, "output.pdf"));
        return 0;
    }
}

Thank you for taking a look!

@dephillips1977

The issue you’re experiencing with the EXIF orientation not being honored during the HTML to PDF conversion is a known limitation in some versions of Aspose.PDF. The library may not automatically apply the EXIF orientation metadata when rendering images, which can lead to the images appearing rotated in the output PDF.

To resolve this issue, you can manually adjust the image orientation before converting the HTML to PDF. Here’s a step-by-step approach you can take:

  1. Load the Image: Use a library like System.Drawing to load the image and read its EXIF orientation.
  2. Rotate the Image: Based on the EXIF orientation value, rotate the image accordingly.
  3. Save the Adjusted Image: Save the rotated image to a temporary file.
  4. Update the HTML: Modify your HTML to reference the adjusted image.
  5. Convert to PDF: Use Aspose.PDF to convert the updated HTML to PDF.

Here’s a minimal code example to illustrate the image rotation:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Aspose.Pdf;

class Program
{
    static void Main()
    {
        string imagePath = "image_exif_orientation_6.jpg";
        string tempImagePath = "adjusted_image.jpg";

        // Load the image and adjust orientation
        using (var image = Image.FromFile(imagePath))
        {
            var orientation = GetExifOrientation(image);
            if (orientation != 1) // 1 means no rotation needed
            {
                RotateImage(image, orientation).Save(tempImagePath, ImageFormat.Jpeg);
            }
            else
            {
                File.Copy(imagePath, tempImagePath, true);
            }
        }

        // Update HTML to use the adjusted image
        string htmlPath = "index.html"; // Update this to reference tempImagePath
        var options = new HtmlLoadOptions(Path.GetDirectoryName(htmlPath));
        using var doc = new Document(htmlPath, options);
        doc.Save("output.pdf");
    }

    static int GetExifOrientation(Image image)
    {
        const int orientationId = 0x0112; // EXIF Orientation tag
        var propId = image.PropertyIdList.FirstOrDefault(id => id == orientationId);
        return propId != 0 ? image.GetPropertyItem(propId).Value[0] : 1; // Default to 1 (no rotation)
    }

    static Image RotateImage(Image img, int orientation)
    {
        // Implement rotation logic based on EXIF orientation
        switch (orientation)
        {
            case 3: // 180 degrees
                img.RotateFlip(RotateFlipType.Rotate180FlipNone);
                break;
            case 6: // 90 degrees clockwise
                img.RotateFlip(RotateFlipType.Rotate90FlipNone);
                break;
            case 8: // 90 degrees counter-clockwise
                img.RotateFlip(RotateFlipType.Rotate270FlipNone);
                break;
        }
        return img;
    }
}

This code snippet demonstrates how to read the EXIF orientation and rotate the image accordingly before saving it. Make sure to adjust your HTML to point to the newly saved image.

Please implement these steps, and if you encounter any issues or have further questions, feel free to reach out for assistance!

Thank you for the quick reply and work around! I have a few follow up questions.

  1. You said “some versions”. I tried the latest version, and observed the same behavior. Are you aware of versions that do not have this known limitation?

  2. Are there any plans on the roadmap to update the library so that it correctly orients images with EXIF metadata?

@dephillips1977

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-60545

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

1 Like

Thanks, Asad. I reviewed the Free Support Policy. One thing is unclear: will this thread be updated when a fix ships, or should we just watch the release notes and retest periodically? Either approach works. I just want to follow the expected process.

@dephillips1977

As soon as the ticket is resolved, you will be receiving a notification via this forum thread about the fixed-in version of the API. In the meanwhile, you can also follow up on the matter here and we will provide updates in this very forum thread as well. Please be patient and spare us some time.

We are sorry for the inconvenience.

1 Like