PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 11:30 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 12 posts ] 
Author Message
PostPosted: Thu Jun 09, 2016 1:26 pm 
Offline

Joined: Wed May 25, 2016 4:19 pm
Posts: 8
Hi,

I am using MigraDoc-WPF (v1.50.4000.0 instaled from nuget) to generate .PDF files from .mdddl templates.
This is the code I use:

Code:
string L_Contrat = //I load my .mdddl as a string here
Document document = DdlReader.DocumentFromString(L_Contrat);
PdfDocumentRenderer renderer = new PdfDocumentRenderer(true);
renderer.Document = document;
renderer.RenderDocument();


In the .mdddl file I load some images (I tried both .png and .jpg, same result) between 3 and 5 depending the mdddl files using the \image("base64:DATA_HERE") syntax.

The problem is that IIS times out, even with the timeout limit extended to 5 minuts it never ends and ISS stops trying.

This line of code is the problem:
Code:
Document document = DdlReader.DocumentFromString(L_Contrat);


I managed to make it work rendering only one/two smaller images but I need to load more slightly bigger images. The issue seems to come from the number and/or size of the images.

The size of the images I load are 30KB, 92KB, 95KB, 115KB, 112KB.
The size of the mdddl file just before rendering (with images in base64 in it) is 679KB

Do you guy know an other method to do this or what could fix my issue ?

Thanks in advance


Last edited by parouuu on Thu Nov 10, 2016 1:07 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Thu Jun 09, 2016 2:06 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
Hi!

It should not take 5 minutes with 5 such small images.

Does the code work on a desktop computer? Do you get any prompts/messages on the desktop computer?

How do you generate the templates? Can you generate PDF directly instead of generating MDDDL?

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Jun 09, 2016 2:17 pm 
Offline

Joined: Wed May 25, 2016 4:19 pm
Posts: 8
Hi,

Thanks for the quick answer.

I managed to make it run on my computer, debugging on Visual Studio (F5) but it still takes around 2 minutes, and the same code doesn't render anything on my Azure hosted web application.
However I don't get any messages, it works after a loooong time.
Again if delete the last four images of the mdddl it generates the PDF instantly, the issue has to come from the image being read/copied/rendered.

The templates are stocked on an Azure stockage server, I download these in a MemoryStream then convert it as a string.

I need this to work with the mdddl format, I have been using this method of generating my PDF for years and I need to update it today only because I'm moving my applications on Azure.

I used to use the DdlReader.DocumentFromFile(filepath) method with image's path stored in the mdddl as \image("C:\\Folder\\IMG.png") and it worked just fine.


Top
 Profile  
Reply with quote  
PostPosted: Thu Jun 09, 2016 2:35 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
One thing you can try: use the MigraDoc source code instead of the NuGet packages and add the existing projects to your solution.

Then you can use a profiler to see which part of MigraDoc consumes such huge amounts of CPU time.

Or provide us with an MDDDL file and let us do the profiling.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Jun 09, 2016 2:55 pm 
Offline

Joined: Wed May 25, 2016 4:19 pm
Posts: 8
Ok, that's a good idea.

Here's the link if you're willing to help me I'll gratefully accept :)

Link to the template

Thank you


Top
 Profile  
Reply with quote  
PostPosted: Thu Jun 09, 2016 3:23 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
I was afraid MigraDoc was using a string in the wrong place.

Class DdlScanner, "protected string ScanStringLiteral()" uses a string.
A StringBuilder should help with the BASE64 file names.

Parsing your sample file takes 23 seconds on my computer - still a long time.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Jun 09, 2016 3:28 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
It's much faster with StringBuilder.

The new method for DdlScanner:

Code:
        protected string ScanStringLiteral()
        {
            Debug.Assert(Char == '\"');
            StringBuilder str = new StringBuilder();
            ScanNextChar();
            while (_currChar != Chars.QuoteDbl && !IsEof(_currChar))
            {
                if (_currChar == '\\')
                {
                    ScanNextChar(); // read escaped characters
                    switch (_currChar)
                    {
                        case 'a':
                            str.Append('\a');
                            break;

                        case 'b':
                            str.Append('\b');
                            break;

                        case 'f':
                            str.Append('\f');
                            break;

                        case 'n':
                            str.Append('\n');
                            break;

                        case 'r':
                            str.Append('\r');
                            break;

                        case 't':
                            str.Append('\t');
                            break;

                        case 'v':
                            str.Append('\v');
                            break;

                        case '\'':
                            str.Append('\'');
                            break;

                        case '\"':
                            str.Append('\"');
                            break;

                        case '\\':
                            str.Append('\\');
                            break;

                        case 'x':
                            {
                                ScanNextChar();
                                int hexNrCount = 0;
                                //string hexString = "0x";
                                while (IsHexDigit(_currChar))
                                {
                                    ++hexNrCount;
                                    //hexString += _currChar;
                                    ScanNextChar();
                                }
                                if (hexNrCount <= 2)
                                    str.Append("?????"); //(char)AscULongFromHexString(hexString);
                                else
                                    throw new DdlParserException(DdlErrorLevel.Error,
                                        DomSR.GetString(DomMsgID.EscapeSequenceNotAllowed), DomMsgID.EscapeSequenceNotAllowed);
                            }
                            break;

                        //NYI: octal numbers
                        //case '0':
                        //{
                        //  ScanNextChar();
                        //  int hexNrCount = 0;
                        //  string hexString = "0x";
                        //  while (IsOctDigit(currChar))
                        //  {
                        //    ++hexNrCount;
                        //    hexString += currChar;
                        //    ScanNextChar();
                        //  }
                        //  if (hexNrCount <=2)
                        //    str += "?????"; //(char)AscULongFromHexString(hexString);
                        //  else
                        //    throw new DdlParserException(DdlErrorLevel.Error, "DdlScanner",DomMsgID.EscapeSequenceNotAllowed, null);
                        //}
                        //  break;

                        default:
                            throw new DdlParserException(DdlErrorLevel.Error,
                              DomSR.GetString(DomMsgID.EscapeSequenceNotAllowed), DomMsgID.EscapeSequenceNotAllowed);
                    }
                }
                else if (_currChar == Chars.Null || _currChar == Chars.CR || _currChar == Chars.LF)
                    throw new DdlParserException(DdlErrorLevel.Error,
                      DomSR.GetString(DomMsgID.NewlineInString), DomMsgID.NewlineInString);
                else
                    str.Append(_currChar);

                ScanNextChar();
            }
            ScanNextChar();  // read '"'
            return str.ToString();
        }

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri Jun 10, 2016 7:50 am 
Offline

Joined: Wed May 25, 2016 4:19 pm
Posts: 8
Hi,

Thanks for your answer, how do I test that new method ? Do I have to download the source code as you suggested before ?


Top
 Profile  
Reply with quote  
PostPosted: Sat Jun 11, 2016 6:38 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
Yes, download the source, reference the projects from the source folders, change the source.

Should be fixed with 1.50 stable, but I think it'll take a few weeks until a new NuGet package will be available.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Fri May 26, 2017 5:47 am 
Offline

Joined: Tue Apr 18, 2017 6:31 pm
Posts: 4
Hi,
I came to the same exact issue but also with Ver. 1.50.4.

I do the follwing through my WPF application:
1- user take snapshot from any where in the screen.
2- image converted to base64 string and saved to xml file.
3- at the time of production PDF file,
4- string base64 is converted to image.
5- image saved to a temp file.
6- migra image created from the temp file.
7- if image width bigger than 15cm it is scaled down to 15 cm width.

that is it. MDDDL File is https://www.dropbox.com/s/gu17r35ub738uzu/tmp922.tmp.mdddl?dl=0.
Thank you for such development. it is indeed helpful

----- Update ------
I realized something, if the snapshot from screen "Clipboard image" is pixilated PDF production crashes. but other wise it works fine. for example I opened an image and did a massive zoom something like 600% and then take a snapshot, this did not work and kept Migra Renderer loops forever, but when I zoom back to a reasonable shot, it worked like charm. but not sure how I can control that with user at the time of taking the shot. but still not sure if this is the reason as it does not make sense.


Top
 Profile  
Reply with quote  
PostPosted: Fri May 26, 2017 6:11 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
Hi!
mostafa90 wrote:
this did not work and kept Migra Renderer loops forever
We have no code to replicate the issue.


mostafa90 wrote:
4- string base64 is converted to image.
5- image saved to a temp file.
6- migra image created from the temp file.
Off topic in this thread. You can pass base64 strings to MigraDoc and let MigraDoc do the decoding without creating a temporary file.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Sat May 27, 2017 1:03 pm 
Offline

Joined: Tue Apr 18, 2017 6:31 pm
Posts: 4
Hi,
Not sure why it is working now... i just restarted my computer.

Regarding the offtopic, I am using this way to calculate the image width before adding to the document, not sure if there is a better way.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 142 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group