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

Corrupted (blank page) when using PDFs from SQL 2008 Reports
https://forum.pdfsharp.net/viewtopic.php?f=3&t=609
Page 1 of 1

Author:  simbolo [ Wed Jan 14, 2009 2:23 am ]
Post subject:  Corrupted (blank page) when using PDFs from SQL 2008 Reports

Scenario:

A Microsoft SQL 2008 Database with Report Services (version 10) generates a PDF report. The report is 1 page long, some basic text, nothing fancy.

Using .Net code, the PDF is opened and saved, the result is a blank PDF (blank as viewed in Adobe PDF Reader 8, i.e., the content has been lost when the PDF goes through PDFSharp).

If I change the document title, default zoom etc, these settings are saved, but the PDF is still blank.

The same report, as generated per the previous version of SQL report services (version 9) runs through PDFSharp normally.

If I try to merge a PDF into the background of that report, like a PDF with a formatted header/footer using the code...

Code:
Dim gfx As XGraphics = XGraphics.FromPdfPage(FirstPage, XGraphicsPdfPageOptions.Prepend)


...an exception is encountered when the above line of code is called. (This error doesn't occour on v9 PDFs, only since v10 was introduced)...


Code:
PdfSharp.SharpZipLib.SharpZipBaseException: Header checksum illegal
  at PdfSharp.SharpZipLib.Zip.Compression.Inflater.DecodeHeader() in PDFsharp\code\PdfSharp\PdfSharp.SharpZipLib\Zip\Compression\Inflater.cs:line 247
  at PdfSharp.SharpZipLib.Zip.Compression.Inflater.Decode() in PDFsharp\code\PdfSharp\PdfSharp.SharpZipLib\Zip\Compression\Inflater.cs:line 427
  at PdfSharp.SharpZipLib.Zip.Compression.Inflater.Inflate(Byte[] buf, Int32 offset, Int32 len) in PDFsharp\code\PdfSharp\PdfSharp.SharpZipLib\Zip\Compression\Inflater.cs:line 711
  at PdfSharp.SharpZipLib.Zip.Compression.Streams.InflaterInputStream.Read(Byte[] b, Int32 off, Int32 len) in PDFsharp\code\PdfSharp\PdfSharp.SharpZipLib\Zip\Compression\Streams\InflaterInputStream.cs:line 409


Microsoft must have done something that upsets PDFSharp with their new rendering engine for PDFs, at this stage I am unsure which side the bug is in.

The PDF version of the report services pdf is 1.3 (Acrobat 4x).

Any suggestions?
Thanks.

Author:  Thomas Hoevel [ Wed Jan 14, 2009 10:32 am ]
Post subject: 

Hi!

We need a PDF file to replicate the problem.

Author:  simbolo [ Wed Jan 14, 2009 2:17 pm ]
Post subject: 

Yes that would be handy, here is the PDF, it's directly out of Report Services 10.

http://cid-6e64738c64f8f959.skydrive.live.com/self.aspx/Public/101O.pdf

To get PDFSharp to make it into a blank PDF...

Code:
Imports PdfSharp
Imports PdfSharp.Pdf
Imports PdfSharp.Pdf.IO
Imports PdfSharp.Drawing

Dim cStatement As PdfDocument = PdfReader.Open(FilePathToPDF)
cStatement.Info.Subject = "An example change."
cStatement.Save(FilePathToPDF)


The subject text is preserved in the blank pdf.

To create the checksum exception...

Code:
Dim lMerge As XPdfForm = XPdfForm.FromFile(OverlayPDFFilePath)
Dim FirstPage As PdfPage = cStatement.Pages(0)
Dim gfx As XGraphics = XGraphics.FromPdfPage(FirstPage, XGraphicsPdfPageOptions.Prepend)

Dim width As Double = FirstPage.Width
Dim height As Double = FirstPage.Height

Dim box As XRect = New XRect(0, 0, width, height)
gfx.DrawImage(lMerge, box)

cStatement.Save(FilePath)


The purpose of the above code is to merge a PDF containing a letterhead onto the target PDF document. The letterhead PDF has been working fine with PDFSharp. The letterhead is only merged onto page 1 of the document.

I hope this is usefull. Thanks.

Author:  simbolo [ Mon Jan 19, 2009 6:44 pm ]
Post subject:  Debug Output

Have been playing around with the Report Services, the only configuration available is to enable compression (as disabled by default), as the error seems related to a ZIP checksum.

Would be terriffic to get this to work, as SQL Report Services 10 can render HTML as PDF, and is free.

I have observed the following debug output messages:

Quote:
UserOrOwnerPasswordRequired: 'At least a user or an owner password is required to encrypt the document.'
UnexpectedToken: 'Token '{0}' was not expected.'
UnknownEncryption: 'The PDF document is protected with an encryption not supported by PDFsharp.'


These messages only appear on the report services 10 PDF, and not the older v9s. There appears to be some unknown symbol (which isn't showing up in the debug message {0}), and curiously, the PDF isn't encrypted or protected.

Still investigating.

Author:  simbolo [ Mon Jan 19, 2009 8:54 pm ]
Post subject:  Cannot trace.

I couldn't trace where the encyption checking was done, but the exceptions seems to originate from Parser.cs

Code:
Symbol ReadSymbol(Symbol symbol)
    {
Symbol current = this.lexer.ScanNextToken();
          if (symbol != current)
             throw new PdfReaderException(PSSR.UnexpectedToken(this.lexer.Token));
          return current;
}


the ScanNextToken seems to encounter an unknown Symbol (not implemented"), the exception occours despite the code returning a Symbol.None.

Unsure how to examine the unrecognised symbol, if I break at the end of ScanNextToken() and look at the value for this.symbol in the VS Locals window, the value is set to "PdfSharp.Pdf.IO.Symbol.BeginStream", here are the other values. Setting to return Symbol.BeginStream instead of Symbol.None has no effect.


Quote:
- this {PdfSharp.Pdf.IO.Lexer} PdfSharp.Pdf.IO.Lexer
currChar 136 '?' char
idxChar 3498 int
nextChar 188 'ΒΌ' char
+ pdf {System.IO.FileStream} System.IO.Stream {System.IO.FileStream}
PdfLength 8825 int
pdfLength 8825 int
Position 3498 int
symbol BeginStream PdfSharp.Pdf.IO.Symbol
Symbol BeginStream PdfSharp.Pdf.IO.Symbol
+ token {} System.Text.StringBuilder
Token "" string
+ TokenToBoolean 'this.TokenToBoolean' threw an exception of type 'System.IndexOutOfRangeException' bool {System.IndexOutOfRangeException}
+ TokenToInteger 'this.TokenToInteger' threw an exception of type 'System.FormatException' int {System.FormatException}
+ TokenToReal 'this.TokenToReal' threw an exception of type 'System.FormatException' double {System.FormatException}
+ TokenToUInteger 'this.TokenToUInteger' threw an exception of type 'System.FormatException' uint {System.FormatException}
ch 136 '?' char


Removing the debug.asserts just results in the code completing without exception, but just giving me a blank PDF (which is larger in size than the original PDF put in).

Is a BeginStream Symbol abnormal?

Author:  simbolo [ Sat Feb 07, 2009 3:38 pm ]
Post subject:  Status update.

I've still not managed to find a solution to this.

Did anyone receive the PDF I sent over?

I notice a similiar Unknown Symbol problem was fixed in the support thread of this forum with just 2 lines of code, so I expect there must be a similiar solution for me?

Author:  arrangemonk [ Tue Nov 10, 2009 11:10 am ]
Post subject:  Re: Corrupted (blank page) when using PDFs from SQL 2008 Reports

found this relating to itextsharp
http://social.msdn.microsoft.com/Forums ... 77950c386f
they say something about zip unrealted tokens in the object stream

maybe it helps

Author:  arrangemonk [ Thu Nov 26, 2009 11:56 am ]
Post subject:  Re: Corrupted (blank page) when using PDFs from SQL 2008 Reports

the only difference between rs22008 pdfs and normal pdfs is simply
where the normal pdf has "stream" the rs2008 pdf has "stream "
with regex or something the rs pdf can be modified and then used

Author:  Ronin [ Mon Apr 26, 2010 3:11 pm ]
Post subject:  Re: Corrupted (blank page) when using PDFs from SQL 2008 Reports

I had exactly the same bug with PDF generated by SSRS and I fixed it by changing Lexer.cs a bit:

old code:
Code:
case "stream":
    return this.symbol = Symbol.BeginStream;

new code:
Code:
case "stream":
    if (this.nextChar == 32 || this.nextChar == 13 || this.nextChar == '\n')
        ScanNextChar();       
    return this.symbol = Symbol.BeginStream;

Author:  marp20x6 [ Mon May 17, 2010 1:40 am ]
Post subject:  Re: Corrupted (blank page) when using PDFs from SQL 2008 Reports

thanks for the pointer Ronin, tested it out and it works a treat, cheers.

Author:  Frank Racis [ Thu Sep 02, 2010 7:53 pm ]
Post subject:  Re: Corrupted (blank page) when using PDFs from SQL 2008 Reports

That fix will work about 99% of the time. However, if your stream data happens to begin with bytes 10, 13, or 32, it will skip over the beginning of your stream.

ReadStream() expects the reader to be positioned at the CR/LF following the "stream" keyword, so you need skip trailing spaces only.

Code:
   case "stream":
      while (ch == 32)
         ch = ScanNextChar();
      return this.symbol = Symbol.BeginStream;

Author:  gerald [ Mon May 09, 2011 8:10 am ]
Post subject:  Re: Corrupted (blank page) when using PDFs from SQL 2008 Reports

My solution - a combination of Ronin's version and the code supplied in [1] - is as follows:

Code:
case "stream":
  if (this.nextChar == 32) ScanNextChar();
  if (this.nextChar == 13) ScanNextChar();
  return this.symbol = Symbol.BeginStream;

HTH someone,
Gerald

[1] Export PDF in ssrs 2008 vs ssrs 2005 - pdf is different, won't work with itextsharp (possibly other PDF post-processing tools)

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