PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

OutOfMemoryException when using gfx.DrawImage() with SVG img
https://forum.pdfsharp.net/viewtopic.php?f=3&t=2612
Page 1 of 1

Author:  she-ext [ Tue Oct 15, 2013 9:40 am ]
Post subject:  OutOfMemoryException when using gfx.DrawImage() with SVG img

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.

Author:  Thomas Hoevel [ Wed Oct 16, 2013 9:12 am ]
Post subject:  Re: OutOfMemoryException when using gfx.DrawImage() with SVG

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.

Author:  she-ext [ Mon Oct 21, 2013 7:15 am ]
Post subject:  Re: OutOfMemoryException when using gfx.DrawImage() with SVG

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 652 times

Author:  she-ext [ Wed Oct 23, 2013 1:14 pm ]
Post subject:  Re: OutOfMemoryException when using gfx.DrawImage() with SVG

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

Author:  mxmissile [ Wed Nov 06, 2013 4:06 pm ]
Post subject:  Re: OutOfMemoryException when using gfx.DrawImage() with SVG

Did you find a solution to this? I am having the same problem. I also tried the WPF version, no luck.

Author:  she-ext [ Tue Nov 12, 2013 8:27 am ]
Post subject:  Re: OutOfMemoryException when using gfx.DrawImage() with SVG

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.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/