boazsegev / combine_pdf

A Pure ruby library to merge PDF files, number pages and maybe more...
MIT License
734 stars 156 forks source link

Combine pdfs is throwing a "stack level too deep" exception #169

Closed edman193 closed 4 years ago

edman193 commented 4 years ago

Hi everyone,

I'm getting a "stack level too deep" with long files when I try to call the to_pdf method, could someone help me, please.

thanks in advance

qvrb commented 4 years ago

Hi, you're going to need to give us a bit more information if someone is to help you. Can you put in the steps to reproduce the issue.

boazsegev commented 4 years ago

Hi @edman193 ,

The combine_pdf library parser and formatter use recursive functions to both load PDF data and format (print out) PDF data.

Long files are not and issue, but need elements are. If your files get too big (to many nested elements), they might explode the stack.

In addition, there are some cases where your code might limit the stack space, so combine_pdf has no "room" (stack memory) to execute its code.

For example, if you're running combine_pdf in a deeply nested code path, it could be that previous functions already consumed too much of the stack.

Or, if you're using Fibers, then your stack will be ~64Kb instead of 2Mb (main thread stack). Another example is that some Ruby versions / releases might still initializes new threads with a 1Mb stack (as opposed to the 2Mb stack allocated to the main thread).

This is why more information is required to solve your specific issue, it might be both caused and solved using different approaches.

edman193 commented 4 years ago

Thanks for the response. My ruby version is 2.6.3 and I notice the issue happened when I tried to attach a file with more than 90 pages to the main file which had about 20 pages. This process runs in a background job. The exception is thrown when I call the to_pdf method.

boazsegev commented 4 years ago

I tried to attach a file with more than 90 pages to the main file which had about 20 pages

Hmm... I worked with more than a thousand pages on CombinePDF without getting the stack to explode. It might be something to do with thecae being used or the specific structure of the files.

What code do you run to combine the PDF documents?

Are you using any "stamp" or overlay?

I suspect somewhere you might be creating cyclic page structure, so that one element of a PDF page references one of its own parent elements as if it were child element, or perhaps referencing itself (creating a cyclic reference).

edman193 commented 4 years ago

Yes, I think it was because of the structure of the files, so thank you!! for your help.

jethrodaniel commented 4 years ago

This may be a good point to add to the readme, in some sort of common issues section.

At least it's in the github issues, if anyone encounters it again.

boazsegev commented 4 years ago

That's a fair point, @jethrodaniel

I'll add a note about this limitation in the readme.

TomK32 commented 1 year ago

I encountered this again, sadly I can't share the pdf file with you. My ruby version is 3.2.0