PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Feb 22, 2018 4:48 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Tue Oct 15, 2013 9:40 am 
Offline

Joined: Tue Oct 15, 2013 9:21 am
Posts: 4
Hello,

I'm getting a System.OutOfMemoryException when I try to add a SVG in my PDF Document.

When searching the forum, I've found several posts about this issue. In one post, a user suggested a fix or patch. Has this been implemented? Has this issue been fixed, and if not: Is this issue going to be fixed?

What I really want to know is: How do I successfully use PDFSharp to generate a PDF with a SVG included?

Below is the code, that fails and the SVG file is attached. What am I doing wrong?


Code:
        public string CreateHelloStaticImageReport ( string outputDir, string inputImageFilename )
        {
            string outputPdfFilename = outputDir + @"\hello-static-image-report-" + Guid.NewGuid() + @".pdf";

            PdfDocument document = new PdfDocument();
            document.Info.Title = "Created with PDFsharp";
            PdfPage page = document.
            XGraphics gfx = XGraphics.FromPdfPage(page);

            XImage image = XImage.FromFile(inputImageFilename);
            //gfx.DrawImage(image, new XRect(0, 0, page.Width, page.Height));
            gfx.DrawImage(image, 0, 0);

            document.Save(outputPdfFilename);
            document.Close();

            return outputPdfFilename;
        }


Yours,
Stephan.


Last edited by she-ext on Mon Oct 21, 2013 7:15 am, edited 3 times in total.

Top
 Profile  
Reply with quote  
PostPosted: Wed Oct 16, 2013 9:12 am 
Offline
empira Employee
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2777
Location: Cologne, Germany
Hi!

No SVG file was attached. A ZIP with a fully functional VS project and the image will make it easier for us to investigate the problem.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Oct 21, 2013 7:15 am 
Offline

Joined: Tue Oct 15, 2013 9:21 am
Posts: 4
Hi,

It appears that your forum doesn't allow files with the extension .svg. Maybe you should look into removing that rule. I've attached a gzip of my svg.

I'm not very familiar with Visual Studio, so I wouldn't know how to make a project from scratch. And since I'm only
working with PDFsharp in a tiny corner of the entire code base, I cannot isolate the code more than I've already done.

I can, however, paste some code that actually compiles ;). Here follows the actual PDFSharpCrashClient with the crashing PDFSharp code, and a PDFSharpCrashClientTest that invokes the crashing code.

I've tried using the latest versions downloaded from your site: PDFSharp-MigraDocFoundation-1_32.zip and PDFSharp-MigraDocFoundation-1_31.zip, and even earlier version too. I've tried both WPF and GDI+. I'm currently using a NuGet package 1.32.3057.0, last published 10/15/2013. All crash with the same message I've stated above.

PDFSharpCrashClient
Code:
namespace ReplacedNamespace
{
    using System;
    using PdfSharp.Drawing;
    using PdfSharp.Pdf;

    public class PDFSharpCrashClient
    {
        public string CreatePdfWithStaticSvgImage ()
        {
            string desktopDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\";
            string inputSvg = desktopDirectory + "chart.svg";
            string outputPdf = desktopDirectory + "chart.pdf";

            PdfDocument document = new PdfDocument();
            document.Info.Title = "Created with PDFsharp";
            PdfPage page = document.AddPage();
            XGraphics gfx = XGraphics.FromPdfPage(page);

            XImage image = XImage.FromFile(inputSvg); // This crashes: System.OutOfMemoryException: Out of memory.
            gfx.DrawImage(image, 0, 0);

            document.Save(outputPdf);
            document.Close();

            return outputPdf;
        }
    }
}


PDFSharpCrashClientTest
Code:
namespace ReplacedNamespace
{
    using Xunit;

    public class PDFSharpCrashClientTest
    {
        [Fact]
        public void ShouldGeneratePdf ()
        {
            string outputPdf = new PDFSharpCrashClient().CreatePdfWithStaticSvgImage();
            Assert.NotNull(outputPdf); // This Assert is never reached because the code above crashes.
        }
    }
}


Attachments:
chart.svg.gz [2.3 KiB]
Downloaded 194 times
Top
 Profile  
Reply with quote  
PostPosted: Wed Oct 23, 2013 1:14 pm 
Offline

Joined: Tue Oct 15, 2013 9:21 am
Posts: 4
Hi,

I need a solution for this.

  1. Is my code correct? I mean, am doing the SVG import wrong? If so, how should I do it?
  2. Is my SVG file bad? Do you have a reference SVG file that is known to work?
  3. Does my code and SVG file work on your system? Do you have a working Visual Studio solution with test case and SVG file that I can try on my system?

I hope you can help me out with this =)

Yours,
Stephan


Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 06, 2013 4:06 pm 
Offline

Joined: Wed Nov 06, 2013 3:32 pm
Posts: 5
Did you find a solution to this? I am having the same problem. I also tried the WPF version, no luck.


Top
 Profile  
Reply with quote  
PostPosted: Tue Nov 12, 2013 8:27 am 
Offline

Joined: Tue Oct 15, 2013 9:21 am
Posts: 4
No, I didn't find a solution, and no one is answering. I think choosing PDFSharp was a mistake.

Someone in our team found a completely different solution that fits our particular problem: phantomjs headless javascript/webkit interpreter/renderer and a script (all free for download somewhere) that can take screenshots from webpages and store them in a pdf document. This way we can generate our documents in HTML/CSS and have them saved as PDF, which is essentially want we want. This has the advantage of using known markup language and styling (HTML and CSS) instead of PDFSharp, iText or whatever. This is particuarly handy when building tables, which are way more fool-proof in HTML than in PDFSharp. And also, SVG works.

http://phantomjs.org/

I'm bailing out.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


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:  
Powered by phpBB® Forum Software © phpBB Group