PDFsharp & MigraDoc Foundation http://forum.pdfsharp.com/ |
|
MigraDoc Issue with Images and Content Streams http://forum.pdfsharp.com/viewtopic.php?f=2&t=4677 |
Page 1 of 1 |
Author: | LjNick [ Thu Sep 19, 2024 12:44 am ] | ||
Post subject: | MigraDoc Issue with Images and Content Streams | ||
I have a project using MigraDoc to generate pdfs on a AspNet Core webserver using .net 6.0. The actual document generation lives in a library project separate from the server project so it can be re-used. Currently with default settings when a pdf is rendered it is blank unless I set CompressContentStreams to false, this is even the case even in a simple hello world type document. Additionally when I attempt to add Images (pngs) from files the image don't appear to be rendering properly a simple red square renders as black and a logo image isn't rendered at all. Here's some relevant code snippets: Code: Library.csproj <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFrameworks>net481;net6.0</TargetFrameworks> </PropertyGroup> <ItemGroup> <PackageReference Include="PDFsharp-MigraDoc" Version="6.1.0" /> </ItemGroup> </Project> Code: WebApp.csproj <Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>net6.0</TargetFramework> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> </PropertyGroup> ... <ItemGroup> <ProjectReference Include="..\Library\Library.csproj" /> </ItemGroup> <ItemGroup> <Content Include="Resources\*.*"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </Content> </ItemGroup> </Project> Code: Code to Build and generate file to be returned to client public Document BuildDocument() { Document document = new Document(); document.UseCmykColor = true; Section section = document.AddSection(); section.PageSetup = document.DefaultPageSetup.Clone(); float pageWidth = (float)(section.PageSetup.PageWidth.Inch - section.PageSetup.LeftMargin.Inch - section.PageSetup.RightMargin.Inch); section.AddParagraph().AddFormattedText("Hello World", _largeFont); var image = section.AddParagraph().AddImage("Resources/google-logo-0.png"); section.AddParagraph().AddImage("Resources/RedSquare.png"); image.Height = 100.0f; section.AddParagraph().AddText("after square"); return document; } public byte[] CreateDocument() { PdfDocumentRenderer renderer = new PdfDocumentRenderer(); renderer.Document = document; renderer.PdfDocument = new PdfSharp.Pdf.PdfDocument(); renderer.PdfDocument.Options.CompressContentStreams = false; renderer.RenderDocument(); byte[] reportBytes; using (MemoryStream ms = new MemoryStream()) { renderer.Save(ms, false); reportBytes = new byte[ms.Length]; ms.Read(reportBytes, 0, (int)ms.Length); } return reportBytes; } The output is attached. The square should be red and there should be a google logo there.
|
Author: | TH-Soft [ Thu Sep 19, 2024 6:04 am ] |
Post subject: | Re: MigraDoc Issue with Images and Content Streams |
Cannot reproduce. We don't even have a PDF to look at. See also: https://docs.pdfsharp.net/General/Issue-Reporting.html It should also work with "renderer.PdfDocument.Options.CompressContentStreams = true;", but we don't have a PDF file generated with that setting. Create files locally on the server and check if these files are OK. Maybe something gets messed up in the transfer between server and browser. You do not show code snippets relevant for that part. Maybe something simple is missing. Like "ms.Position = 0" before reading the stream. "ms.GetBuffer()" could be more efficient here. "ms.ToArray()" is easier to use, but less efficient. See also: https://www.pdfsharp.net/wiki/Clock-sample.ashx |
Author: | TH-Soft [ Fri Sep 20, 2024 1:59 pm ] |
Post subject: | Re: MigraDoc Issue with Images and Content Streams |
Some after thoughts: Uncompressed streams only contain byte values in the range 32 to 126, so code page problems should not make a difference. Compressed streams contain byte values from 0 to 255 and there is a CRC for the stream. If a single bit is altered, there will be a CRC error on decompression and the stream will be ignored and the page shows empty. Long story short: Most likely the PDF file gets altered on the way to the client, leading to invalid images and empty pages. |
Author: | LjNick [ Sun Sep 22, 2024 5:27 pm ] |
Post subject: | Re: MigraDoc Issue with Images and Content Streams |
Thankyou for your quick response and I'm sorry for not submitting this originally via the proper issue reporting way, I was in a rush and missed seeing the template - I did try and attach a sample pdf but it was too large too attach. I did attempt to save to the server but the file saved is not altered when it is sent to the client. I am in the process of making a stripped down project(s) to make a MCVE as currently the project has a lot of moving parts that make it hard to submit as is. |
Author: | TH-Soft [ Mon Sep 23, 2024 12:29 pm ] |
Post subject: | Re: MigraDoc Issue with Images and Content Streams |
It does not matter where you report your issue and using the IssueSubmissionTemplate is not mandatory. But it is essential that we can replicate the issue. Small PDFs can be attached here if they are zipped, as explained on the Forum Rules page. |
Author: | LjNick [ Wed Sep 25, 2024 3:25 pm ] |
Post subject: | Re: MigraDoc Issue with Images and Content Streams |
It does look like their was a error with the stream content getting altered on save; however, I cannot pin point for the life of me where or how as when I was comparing a valid test pdf and a invalid one everything in the streams appeared to match according to my compare tool . What was also weird was when I saved the file to disk then sent it to the client the file saved on disk was also invalid (maybe I was checking the wrong file ). Anyways the solution seemed to be changing how the file was being requested from the server on the client side and that resolved the issue thankyou very much for your feedback and help . |
Author: | TH-Soft [ Wed Sep 25, 2024 6:17 pm ] |
Post subject: | Re: MigraDoc Issue with Images and Content Streams |
Smart compare tools ignore differences in line endings (like LF vs. CR/LF). Did you see differences of the file sizes? There still is the simple "FC /B" command available in the Windows console. |
Author: | LjNick [ Wed Sep 25, 2024 8:41 pm ] |
Post subject: | Re: MigraDoc Issue with Images and Content Streams |
I didn't notice at the time but I suspect that was the issue then, thank you again for your help |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |