PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 7:31 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: Sun May 14, 2023 9:31 pm 
Offline

Joined: Sun May 14, 2023 9:22 pm
Posts: 2
The below code was working perfectly fine with pdfsharp-migradoc (1.50.4820-RC1). I recently migrated it to pdfsharp-migradoc (6.0.0-preview-2) and I get the error "Image has no valid type" when I open the pdf
Attachment:
pdf error.PNG
pdf error.PNG [ 9.04 KiB | Viewed 4168 times ]


I have an image in base64 format which I add to the document using the following code
Code:
private static void AddLabels(Document document, IList<string> images)
        {
            var section = document.LastSection;

            // Add an image
            for (int i = 0; i < images.Count; ++i)
            {
                if (i > 0)
                {
                    section.AddPageBreak();
                }
                var image = section.AddImage("base64:" + images[i]);
                image.LockAspectRatio = true;
                image.Height = "4in";
            }
        }

Code:
AddLabels(document, images);
var renderer = new PdfDocumentRenderer
            {
                // Associate the MigraDoc document with a renderer.
                Document = document,
                PdfDocument = new PdfDocument()
            };
 renderer.RenderDocument();


Top
 Profile  
Reply with quote  
PostPosted: Mon May 15, 2023 8:07 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
amulya wrote:
The below code was working perfectly fine with pdfsharp-migradoc (1.50.4820-RC1). I recently migrated it to pdfsharp-migradoc (6.0.0-preview-2) and I get the error "Image has no valid type" when I open the pdf
Which versions and flavours of MigraDoc are you using?
What can we say without having the image (image file, not BASE64 encoded).

The list of supported formats can be seen here:
https://docs.pdfsharp.net/PDFsharp/Over ... ge-formats

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Wed Jan 10, 2024 12:41 am 
Offline

Joined: Wed Jan 10, 2024 12:28 am
Posts: 5
I'm encountering this issue too. I've tried using AddImage() with the base64: prefix for both BMP and PNG formats with no success.
The image in question is a QR code representing "https://www.example.com", which I've attached (in PNG format).

I'm using the PDFsharp-MigraDoc nuget package, version 6.0.0 (not GDI).

Here's the code I'm using to generate the QR code base-64 string (requires QRCoder nuget package version 1.4.3):

Code:
var qrGenerator = new QRCodeGenerator();
var qrData = qrGenerator.CreateQrCode("https://www.example.com", QRCodeGenerator.ECCLevel.Q);
var qrCode = new BitmapByteQRCode(qrData);
var qrCodeImage = qrCode.GetGraphic(pixelsPerModule: 8);
var base64 = Convert.ToBase64String(qrCodeImage);
paragraph.AddImage("base64:" + base64);


The same code worked fine with the GDI build of PDFsharp-MigraDoc (6.0.0), but I've had to switch to the platform-agnostic version due to difficulties targeting net6.0-windows in a web application (as opposed to just targeting net6.0).


Attachments:
File comment: Example-QR
Example-QR.png
Example-QR.png [ 7.38 KiB | Viewed 3278 times ]
Top
 Profile  
Reply with quote  
PostPosted: Wed Jan 10, 2024 8:39 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
Extragorey wrote:
I'm encountering this issue too. I've tried using AddImage() with the base64: prefix for both BMP and PNG formats with no success.
The image in question is a QR code representing "https://www.example.com", which I've attached (in PNG format).
I converted it to BMP using MS Paint and it works as expected.
How did you convert to BMP?


Attachments:
Example-QR.zip [480 Bytes]
Downloaded 174 times

_________________
Regards
Thomas Hoevel
PDFsharp Team
Top
 Profile  
Reply with quote  
PostPosted: Wed Jan 10, 2024 8:42 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
Extragorey wrote:
I'm encountering this issue too. I've tried using AddImage() with the base64: prefix for both BMP and PNG formats with no success.
The image in question is a QR code representing "https://www.example.com", which I've attached (in PNG format).
The PNG file you attached here works with PDFsharp. This format is supported.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 11, 2024 12:15 am 
Offline

Joined: Wed Jan 10, 2024 12:28 am
Posts: 5
Thomas Hoevel wrote:
Extragorey wrote:
I'm encountering this issue too. I've tried using AddImage() with the base64: prefix for both BMP and PNG formats with no success.
The image in question is a QR code representing "https://www.example.com", which I've attached (in PNG format).
The PNG file you attached here works with PDFsharp. This format is supported.

The image I previously attached was actually a BMP that I had to convert to PNG (using image software) because the forum doesn't support BMP uploads. I've now changed my code to generate a PNG image instead (and removed the white border), and have included that image with this post. Apologies for the confusion.

You can also reproduce it with the code I provided - just in case the forum processes image uploads somehow and doesn't preserve the original byte code.

(Edit: I've just re-downloaded the image below and verified it has the same byte representation as the image that fails to load in MigraDoc.)

The weird thing is, the same document renders a barcode in BMP format from memory just fine (from the BarcodeLib nuget library), using the base64: prefix, but there must be something different about the way the QRCoder library generates images that is incompatible with MigraDoc. I've also tried copying the base-64 representation into HTML to render the image there (i.e. <img src="data:image/png;base64,[bytes]" /> which it renders as expected), then save that as a PNG file and import the file using MigraDoc, and that doesn't work either. So it's not specific to the base64: implementation, but something about the image format it seems.


Attachments:
File comment: QR code in PNG format
Example-QR.png
Example-QR.png [ 318 Bytes | Viewed 3267 times ]
Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 11, 2024 9:04 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
Extragorey wrote:
The weird thing is, the same document renders a barcode in BMP format from memory just fine (from the BarcodeLib nuget library), using the base64: prefix, but there must be something different about the way the QRCoder library generates images that is incompatible with MigraDoc.
As of today, PDFsharp Core build supports monochrome BMP images, but does not support monochrome PNG images. That's an implementation limitation.
Convert the monochrome PNG image to a monochrome BMP image or turn it into a color-PNG image and it will work.
Using a monochrome BMP will have no impact on the size.

https://docs.pdfsharp.net/PDFsharp/Over ... tions.html

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 11, 2024 11:13 pm 
Offline

Joined: Wed Jan 10, 2024 12:28 am
Posts: 5
TH-Soft wrote:
Extragorey wrote:
The weird thing is, the same document renders a barcode in BMP format from memory just fine (from the BarcodeLib nuget library), using the base64: prefix, but there must be something different about the way the QRCoder library generates images that is incompatible with MigraDoc.
As of today, PDFsharp Core build supports monochrome BMP images, but does not support monochrome PNG images. That's an implementation limitation.
Convert the monochrome PNG image to a monochrome BMP image or turn it into a color-PNG image and it will work.
Using a monochrome BMP will have no impact on the size.

https://docs.pdfsharp.net/PDFsharp/Over ... tions.html

The BMP is what I tried first though, and that didn't work either. I've uploaded the full byte representation here, since the forum doesn't allow BMP uploads (and most image hosts convert it to PNG):

https://justpaste.it/85i6r

It makes sense there's a difference between monochrome and colour formats though, I didn't think of that. I may have to try doing some additional image processing to get it to work.


Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 12, 2024 2:54 am 
Offline

Joined: Wed Jan 10, 2024 12:28 am
Posts: 5
I've found the issue! The images I was trying to render (both BMP and PNG formats) had a pixel format of PixelFormat.Format1bppIndexed, which MigraDoc apparently doesn't like. I converted it to PixelFormat.Format32bppArgb (using System.Drawing, since I only need the code to run on Windows) and viola, the image renders.

Here's my helper function to perform the conversion:

Code:
    // Required namespaces
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;

    private static byte[] ConvertPngToArgb(byte[] imageData) {
        var ms = new MemoryStream(imageData);
        var image = new Bitmap(ms);
        var newImage = image.Clone(new Rectangle(0, 0, image.Width, image.Height), PixelFormat.Format32bppArgb);
        var newMs = new MemoryStream();
        newImage.Save(newMs, ImageFormat.Png);
        return newMs.ToArray();
    }


Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 12, 2024 8:11 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
Extragorey wrote:
I've found the issue! The images I was trying to render (both BMP and PNG formats) had a pixel format of PixelFormat.Format1bppIndexed, which MigraDoc apparently doesn't like.
1 BPP works in BMP format, but not in PNG format.
1 BPP works for PNG when using the GDI or WPF builds. And "since I only need the code to run on Windows" you can use one of those builds.

You might get smaller PDF files when converting to 1 BPP BMP instead of 32 BPP PNG. And 8 BPP PNG is also supported and may also lead to smaller PDF files.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 16, 2024 4:16 am 
Offline

Joined: Wed Jan 10, 2024 12:28 am
Posts: 5
TH-Soft wrote:
Extragorey wrote:
I've found the issue! The images I was trying to render (both BMP and PNG formats) had a pixel format of PixelFormat.Format1bppIndexed, which MigraDoc apparently doesn't like.
1 BPP works in BMP format, but not in PNG format.
1 BPP works for PNG when using the GDI or WPF builds. And "since I only need the code to run on Windows" you can use one of those builds.

You might get smaller PDF files when converting to 1 BPP BMP instead of 32 BPP PNG. And 8 BPP PNG is also supported and may also lead to smaller PDF files.

You're right, the BMP's pixel format was Format24bppRgb, not 1 BPP. Annoying how the QRCoder library's output is so inconsistent. I've now changed it to use Format8bppIndexed, which seems to work.

My initial approach was to use the GDI build as you suggest, which greatly simplifies font and image handling, but it forces projects to target net6.0-windows which has massive caveats for a web application, even when only running it on Windows. Namely, it doesn't even use the same .NET 6 runtime, but the "Desktop Runtime" instead. This also seems to prevent on-the-fly updates/deployments as the application's DLL files are locked by the system. All fine and dandy for a WinForms application, perhaps, but not for a web API hosted in IIS.

I found all this out through a long, frustrating process of trial and error, as you would think there's no harm in targeting net6.0-windows in an application that's only going to run on Windows. Big mistake.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 133 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group