Not able to convert from PDF to SVG via stream

@chiraagbangera

Could you kindly share the screenshot of the error with us. We tested the scenario using your file and did not notice any error or exception at var svgDocument = new SVGDocument(ms, ".");. We spectated the object and found that ChildNodes and Title Properties were NULL. While loading the SVG file from a file, the Title Property of the SVGDocument had some value.

Hi Asasd, I did not get any error, but when inspecting the svgdocument object, most of the properties were null including “ChildNodes”, “RootElement”. This is what lead me to believe that the svgdoucment object being created is empty/invalid. However, saving the file as a svg and then loading the file in as a file stream works perfectly.

image.png (33.1 KB)

image.png (37.2 KB)

@chiraagbangera

Please add following line of code before loading the SVGDocument from MemoryStream and let us know if issue still persists:

ms.Seek(0, SeekOrigin.Begin);

Hi Asad, that worked! Since this was not documented in the official documentation would this classify as a bug ?

@chiraagbangera

This would not be a bug in the API as seeking the memory stream back to the beginning resolved the issue. However, we will surely update our documentation accordingly so that such a situation can be prevented in the future.

@chiraagbangera

This to update you that we are going to add descriptions to API that document is processed from the current position in the stream. We think that it will be enough because working with positions in streams is a regular practice described in Microsoft documentation. Besides you are able to load documents from compound streams where position of document within the stream can be more than 0.

Thanks Asad, I agree.

1 Like

I have an additional question related to the same project when used as a managed library inside Unity3D. When I am loading the SVGDocument either through a stream or a file, it doesn’t seem to fully initialize. I end up with an object that is empty. The ready state says loading and doesn’t seem to change to complete. This is only in Unity3D, the unit tests work perfectly fine.

image.png (22.8 KB)
Is there something I am missing ?

@chiraagbangera

It is hard to tell the reason behind such behavior by the API. Would you please share the steps to replicate the same issue in our environment. Also, please share your sample SVG file with us. We will test the scenario in our environment and address it accordingly.

Hi Asasd, i have created a sample unity3d project and a sample .net standard library that is used in the unity project. I still have the same issue when using the sample file. I have attached everything needed to test with this message.

When inspecting the svgdocument object after creating a new instance via stream, the ReadyState still says loading. I already tried loading the document asynchronously with the example code from

but that creates an infinite loop at the documentEvent.WaitOne(); line and unity freezes obviously.

Also,
I tried loading a pdf/svg directly from the unity3d project with the code below, no luck. Changing unity player settings such as scripting backend and .net version also yielded no results.

var svgDocuments = new List();
var document = new Document(pdf);
foreach ( var page in document.Pages )
{
// Extract each page as a separate pdf document for conversion
var pageDocument = new Document();
pageDocument.Pages.Add(page);

        // Convert page into svg document for manipulation
        using ( var ms = new MemoryStream() )
        {
            pageDocument.Save(ms, SaveFormat.Svg);
            // Reset stream position to 0
            ms.Seek(0, SeekOrigin.Begin);

            var svgDocument = new SVGDocument(ms, ".");
            svgDocuments.Add(svgDocument);
        }
    }

    // Load SVG Directly from file
    var testSvg = new SVGDocument(".\\Assets\\temp.svg"); //same issue here

I do want to add that the pdf loads fine, i am able to save it as a file pdf/svg and it looks okay to me, it’s only when creating the svgdocument i have issues.

@chiraagbangera

We are checking the scenario in our environment with Unity3D and will get back to you shortly.

Sounds good. Thanks.

Hi, any updates to this issue yet ?

@chiraagbangera

We are in process of investigation and will get back to you in a while. Please give us some time.

Thanks @asad.ali, I understand, Please prioritize this issue as it is currently a blocker for us and we are on a deadline for our product launch this month. A workaround to get it working in Unity3D is also acceptable.

@chiraagbangera

An investigation ticket as SVG-100 has been logged in our issue tracking system for your scenario. We will further look into its details and inform you as soon as it is resolved. Please give us some time.

We apologize for the inconvenience.

@asad.ali Thanks, hoping for a speedy bug fix or workaround.

@chiraagbangera

We have found the next reason for this issue: Some encodings (including popular Windows-1252) are defined in separate NuGet packages and these encodings are not available by default on .NET Core.

Workaround:

  1. You can use Aspose.SVG netstandard2.0 assembly. (it is available on the path packages\aspose.svg\20.12.0\lib\netstandard2.0\Aspose.SVG.dll). We have found that you use NET4.0 framework instead.
  2. You can add next code to the program:
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

Like this:

void Start() 
{
 Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
 var pdf = ".\\Assets\\Sample.pdf";
 var document = Converter.Convert2SVG(pdf);
 foreach (var page in document)
 {
  var svgElement = page.RootElement;
 } 
} 

And it is necessary to add the next libraries to Assets:

  • System.Runtime.CompilerServices.Unsafe.dll
  • System.Security.Permissions.dll
  • System.Text.Encoding.CodePages.dll

More information can be found here:

@asad.ali Thank you, that worked. I can confirm that it works for .NET 4.x scripting backend in Unity3D too. However, I did have to do the other steps such as download the required dependencies and also register the text encodings.

@chiraagbangera

It is good to know that your issue has been resolved by suggested workaround. Please keep using our API and feel free to create a new topic in case you need further assistance.