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

Document corrupt every few times
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3646
Page 1 of 1

Author:  Silverfang [ Thu Aug 24, 2017 7:59 am ]
Post subject:  Document corrupt every few times

Hello everyone,

first i have to say sorry for my bad english and i hope my problem description is understandable.

In one of my VB.net projects i create daily reports for my company and these reports contain a very simple frontpage (just one picture, two lines of text under the picture and one line footer-text).

The following example demonstrates the simple layout:

Attachment:
frontpage.JPG
frontpage.JPG [ 11.01 KiB | Viewed 8080 times ]


The most times (9/10) the frontpage ist generated fine, but at the 10th time the PDF-file becomes corrupt. Instead of one frontpage the file displays the frontpage as two empty pages with a height of 1px and Adobe reader fires a warning (page out of range). iphones totally refuse to open the document.

Attachment:
Defekt.JPG
Defekt.JPG [ 15.59 KiB | Viewed 8080 times ]


The following pages are more complex (toc, tables with calculated pagebreaks and so on) but everything works fine here. If you scroll down in the "corrupt" file the following pages are displayed correctly.

The following code sample shows how i create the document.

Code:
        Private Sub Generate()
            Dim Started As DateTime =DateTime.Now
            Try               
                _Logger.Info("started") 'Log4Net Logger
                GetValues()
                CreateDoc()
                AddFrontPage() '<-- Here the Problem seems to occure
                AddTOC()
                AddTable1()
                AddTable2()
                AddTable3()
                AddTable4()
                AddTable5()
                RenderAndSendMail()
                _Logger.Info(_Rec.ShopGroupToProcess.GroupDescription & " - generated in " & DateTime.Now.Subtract(Started).TotalSeconds.ToString("#,##0") & " seconds")
            Catch ex As Exception
                Throw New Exception(ex.Message)
            End Try
        End Sub


Code:
        Private Sub CreateDoc()
            Try
                _Doc = New Document
                _Doc.Info.Title = "Report for " & _Rec.ShopGroupToProcess.GroupDescription & " " & _MaxDate.ToString("dd.MM.yyyy")
                _Doc.Info.Author = "Automated report generator by..."
            Catch ex As Exception
                Throw New Exception(".CreateDoc - " & ex.Message)
            End Try
        End Sub


Code:
        Private Sub AddFrontPage()
            Try
                Dim Sec As Section = _Doc.AddSection
                Dim Setup As PageSetup = _Doc.DefaultPageSetup.Clone

                With Setup
                    .TopMargin = Unit.FromCentimeter(3.5)
                    .LeftMargin = Unit.FromCentimeter(1)
                    .RightMargin = Unit.FromCentimeter(1)
                    .BottomMargin = Unit.FromCentimeter(1.5)
                    .Orientation = Orientation.Landscape
                    .PageFormat = PageFormat.A4
                End With

                Sec.PageSetup = Setup

                Dim Para As Paragraph = Sec.AddParagraph()
                Para.Format.Alignment = ParagraphAlignment.Center
                Para.Format.SpaceBefore = Unit.FromCentimeter(3)
                Para.AddImage("Images\CompanyLogo.png")
               
                Para = Sec.AddParagraph("Report for " & _MaxDate.ToString("dd.MM.yyyy"))           
                Para.Format.Font.Size = Unit.FromPoint(15)
                Para.Format.Font.Color = Colors.DarkGreen
                Para.Format.Font.Name = "Arial Black"
                Para.Format.Alignment = ParagraphAlignment.Center

                Para = Sec.AddParagraph(_Rec.ShopGroupToProcess.GroupDescription)
                Para.Format.Font.Size = Unit.FromPoint(15)
                Para.Format.Font.Color = Colors.DarkGreen
                Para.Format.Font.Name = "Arial Black"
                Para.Format.Alignment = ParagraphAlignment.Center

                Para = Sec.Footers.Primary.AddParagraph("Generiert am: " & DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"))
                Para.Format.Alignment = ParagraphAlignment.Right
                Para.Format.Font.Size = Unit.FromPoint(8)
            Catch ex As Exception
                _Logger.Error(_Rec.ShopGroupToProcess.GroupDescription & ".AddFrontPage - " & ex.Message)
                Throw New Exception
            End Try
        End Sub


Code:
        Private Sub RenderAndSendMail()
            Try
                Dim Rend As New MigraDoc.Rendering.PdfDocumentRenderer
                Rend.Document = _Doc
                Rend.RenderDocument()
                'No difference if true or false
                If _PDFInMemory Then
                    Using Stream As New MemoryStream
                        Rend.Save(Stream, False)
                        SendMail(Stream)
                    End Using
                Else
                    If Not Directory.Exists(_SaveFolder) Then
                        Directory.CreateDirectory(_SaveFolder)
                    End If
                    Rend.Save(_SavePath)
                    SendMail()
                End If
            Catch ex As Exception
                _Logger.Error(_Rec.ShopGroupToProcess.GroupDescription & ".RenderAndSendMail - " & ex.Message)
                Throw New Exception
            End Try
        End Sub


Could somebody help me with the problem? I'm a bit confused.

Thanks.

Author:  Thomas Hoevel [ Thu Aug 24, 2017 8:15 am ]
Post subject:  Re: Document corrupt every few times

Hi!

As a matter of principle we do not build test apps from code snippets.
If you can provide a VS solution that allows us to replicate the issue (1 out of 10 times or 1 out of 20 times) with PDFsharp 1.50 beta 4, then we will investigate the issue.

See also:
viewtopic.php?f=2&t=832

Author:  Silverfang [ Thu Aug 24, 2017 8:34 am ]
Post subject:  Re: Document corrupt every few times

Hi,

thanks for the fast reply.
I forgot to mention that i'm still using Version 1.3.4334 as NuGet-Package.

I try to replicate the Problem in an example solution.

Thx.

Author:  Silverfang [ Thu Aug 24, 2017 10:31 am ]
Post subject:  Re: Document corrupt every few times

Hi,

i've created a simple example-solution with version 1.50.4589-beta4 which replicates the problem.

This solution creates multiple files and the chance is high that on of these files is "corrupt".

Thx for the help.

Attachments:
MigradocTest.zip [58.83 KiB]
Downloaded 318 times

Author:  Thomas Hoevel [ Thu Aug 24, 2017 12:21 pm ]
Post subject:  Re: Document corrupt every few times

Thanks for the ZIP. I think I'll try it tonight or tomorrow.

Have you tried 1.50.4000-beta3b or 1.32.2608?

Author:  Silverfang [ Thu Aug 24, 2017 12:44 pm ]
Post subject:  Re: Document corrupt every few times

Quote:
Thanks for the ZIP


I have to say thank you, for your help. The chance is high that the error is my own fault, so.... ;-)

Quote:
Have you tried 1.50.4000-beta3b or 1.32.2608?


The actual productive version uses 132.4334 and the example uses 1.50.4589-beta4.
I didn't try 1.50.4000-beta3b or 1.32.2608.

I've got a sneaking suspicion that my multitasking via TPL causes some problems.
Because i have to create many different files (different grouping for different company parts) i generate some of the files simultaniously via task.awaitall. But i wonder, that no exception is thrown...

Enclosed you find two examples of PDF-Files. A working one and a corrupt one.

Attachments:
PDF examples.zip [126.88 KiB]
Downloaded 309 times

Author:  Thomas Hoevel [ Thu Aug 24, 2017 1:41 pm ]
Post subject:  Re: Document corrupt every few times

Silverfang wrote:
The chance is high that the error is my own fault, so.... ;-)
Multitasking can be a problem.

The critical part should be "RenderAndSendMail()".
If serializing calls to "RenderAndSendMail()" solves the issue then the problem is related to multitasking. Maybe AppDomain will be a suitable workaround.

If 1.32 also fails then the problem is not with my latest changes <sigh>.

Author:  Silverfang [ Thu Aug 24, 2017 2:55 pm ]
Post subject:  Re: Document corrupt every few times

Hi Thomas,

thanks for the hint.

Removing the multitasking completely seems to resolve the problem.

I've tested the creation of 50 Files als single tasks one after one and all files seems to be ok (executed 4 times, so 200 files were ok).

Code:
            Dim DirPath As String = Path.Combine(My.Application.Info.DirectoryPath, "OutFiles")
            Dim Generator As New TestGenerator

            For i As Integer = 1 To 50
                Generator = New TestGenerator With {
                         .SaveDirectoryPath = DirPath,
                         .DocName = "TestDoc" & i.ToString & ".pdf",
                         .Suffix = i.ToString
                }
                Await Generator.GenerateAsync
                Process.Start(Path.Combine(DirPath, Generator.DocName))
            Next


By the way, to use multithreading for the drawing itself (CreateDoc, AddFrontPage, AddTable and so on) and then just call RenderAndSave one by one as single task doesn't work either.

I have to thank you again for your help.

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