ststeiger / PdfSharpCore

Port of the PdfSharp library to .NET Core - largely removed GDI+ (only missing GetFontData - which can be replaced with freetype2)
Other
1.08k stars 237 forks source link

The specified file has no valid PDF file header #12

Closed sanglanun closed 6 years ago

sanglanun commented 6 years ago

Hi,

I'm using your PdfSharp. It really awesome because working with .Net Core.

But now I get an error like below: ArgumentException: The specified file has no valid PDF file header. Parameter name: path

It happen when using below code: XPdfForm form = XPdfForm.FromFile(filePath);

I check in the code, I suspect it because of the files in the PdfReader at line 162 like below: string header = PdfEncoders.RawEncoding.GetString(bytes, 0, bytes.Length);

Whenever I using this, it always return PDF version as 0. So when I try using original code string header = Encoding.ASCII.GetString(bytes);, it working like charm, it will return with PDF version.

So, how do fix this? Or do you have suggestion for this? Or you can replace it using this below code string header = Encoding.ASCII.GetString(bytes);

ststeiger commented 6 years ago

I'm not sure if this is correct, the original code does not use ASCII. Probably the file has been saved with a wrong encoding.

We can do this, but it's probably better positioned inside the catch than used as default.

sanglanun commented 6 years ago

Thanks for reply. Not sure about it, but i saw in version 1.32, it using ASCII. Anyway, hope you help me to update this and inside NuGet. Really appreciate your help. :-)

ststeiger commented 6 years ago

@sanglanun: Merged your pull-request. Changed it to use RawEncoding first, and if that doesn't work, use ASCII. Could you check if that works for you ?

@startnow65: If you'd be so kind, could you update the NuGet package ?

startnow65 commented 6 years ago

@ststeiger The NuGet package would be updated in about 2hrs as i'm currently away.

ststeiger commented 6 years ago

No stress. We have all the time in the world ;)

startnow65 commented 6 years ago

Great, NuGet updated..

sanglanun commented 6 years ago

Hi Guys, sorry for a late reply. Away from work yesterday. I already try now, it still give me a '0' as a file version. Now, I not sure where is the problem.

Below is my code to test pdf file: string filePath = @"D:\sample.pdf"; var pdfversion = PdfReader.TestPdfFile(filePath); // give 0

Then I try using from this NuGet in other test web app, tried the same code like above but it gave me '14' as pdf version.

Maybe the problem in the line 72 or 118. public static int TestPdfFile(string path)

When comparing your code with the original code, there is 1 line different in the TestPdfFile method. It was in line 74. #if !NETFX_CORE && !PORTABLE

Anyway, really appreciate your work and time. 👍

ststeiger commented 6 years ago

Ah, looked at it. There is indeed another bug. For some reason, it defines away the entire routine if NETFX_CORE is defined... Quick-Fixed that. @startnow65: Can you Nuget this again ?

@sanglanun: Check if this works now (from source - or from nuget after startnow uploaded the new package). If it doesn't work afterwards, can you upload the sample.pdf somewhere ?

sanglanun commented 6 years ago

Awesome. I will wait for the nuget. Still new in the Asp .Net, really appreciate your time. Anyway, I already try a lot pdf even from adobe site, still got '0' result. Hopefully it will work this time. :)

startnow65 commented 6 years ago

Hey friends, so sorry for the delay, had some issues. The nugget is live now

I may have to automate this process sometime soon...

ststeiger commented 6 years ago

Haha, yea you will (already wondered when you were going to say that). Let me know if you need any help ;)

sanglanun commented 6 years ago

Awesome guys. It works now. Thanks for your time and effort. 💯