PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Nov 14, 2024 11:01 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Thu Sep 19, 2024 12:44 am 
Offline

Joined: Thu Sep 19, 2024 12:02 am
Posts: 4
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.


Attachments:
File comment: Generated pdf screenshot
Generated.png
Generated.png [ 6.08 KiB | Viewed 10960 times ]
Top
 Profile  
Reply with quote  
PostPosted: Thu Sep 19, 2024 6:04 am 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1009
Location: CCAA
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

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


Top
 Profile  
Reply with quote  
PostPosted: Fri Sep 20, 2024 1:59 pm 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1009
Location: CCAA
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.

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


Top
 Profile  
Reply with quote  
PostPosted: Sun Sep 22, 2024 5:27 pm 
Offline

Joined: Thu Sep 19, 2024 12:02 am
Posts: 4
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.


Top
 Profile  
Reply with quote  
PostPosted: Mon Sep 23, 2024 12:29 pm 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1009
Location: CCAA
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.

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


Top
 Profile  
Reply with quote  
PostPosted: Wed Sep 25, 2024 3:25 pm 
Offline

Joined: Thu Sep 19, 2024 12:02 am
Posts: 4
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 :oops:).

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 :mrgreen:.


Top
 Profile  
Reply with quote  
PostPosted: Wed Sep 25, 2024 6:17 pm 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1009
Location: CCAA
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.

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


Top
 Profile  
Reply with quote  
PostPosted: Wed Sep 25, 2024 8:41 pm 
Offline

Joined: Thu Sep 19, 2024 12:02 am
Posts: 4
I didn't notice at the time but I suspect that was the issue then, thank you again for your help


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

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 38 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