brentnycum / BNHtmlPdfKit

Easily turn HTML data from an HTML string or URL into a PDF file on iOS
MIT License
270 stars 62 forks source link

iOS8 hangs at [renderer numberOfPages] #12

Open cliffjoyce opened 10 years ago

cliffjoyce commented 10 years ago

On iOS 8, the _savePdf method often (but not always) hangs at [renderer numberOfPages]. The more complex the page, the more likely the hang.

ChrisBeeson commented 10 years ago

I'm having the same issue... have you found a fix?

cliffjoyce commented 10 years ago

It turns out that there's a CSS property called "page-break-after". When the source HTML includes this property, then the renderer hangs when attempting to calculate the numberOfPages-- but only in iOS 8 (not iOS 7 or 6). We use this CSS property to create HTML page breaks.

From the _savePdf method, we've tried setting various values for self.webView.paginationMode and self.webView.paginationBreakingMode. But those changes did not prevent the hang.

Our current thinking that we need to modify our HTML to exclude the "page-break-after" CSS, and devise some other way to control our HTML page breaks.

ChrisBeeson commented 10 years ago

Personally I'm not using that CSS property, but it still hangs.

It seems to be a new issue with UIPrintPageRenderer - (void)drawPageAtIndex:(NSInteger)pageIndex inRect:(CGRect)printableRect;

melsam commented 10 years ago

Thanks @cliffjoyce for the suggestion. Confirming that removing page-break-after solved the issue for me too. In my app, I removed page-break-after from one element and added page-break-before to another element to achieve the same result. This does seem to be an Apple bug in iOS 8, but at least the workaround seems to work.

cliffjoyce commented 10 years ago

Thanks back at you @melsam. Your suggestion to use page-break-before resolved our issue as well.

bcriscuolo commented 9 years ago

Thanks to this thread, you folks got me out of a big jam. :)

iOS 9 still has the issue, including in the simulator.

My fix (client-side) was to replace any instances of "page-break-after" with the newer "break-after" CSS property prior to loading my web view; fully compatible and now working correctly.

esutton commented 7 years ago

Thank you all!

The fix for me was to use suggestion similar to @bcriscuolo and change page-break-before to the newer "break-before" CSS property

const QString CssStyleSheet =
        "<style type=\"text/css\">"
        "@media print {"
        ".pagebreak-before:first-child { display: block; page-break-before: avoid; }"
        ".pagebreak-before { display: block; page-break-before: always; }"
        "}"
        ".odd {"
        "background-color: #efefef;"
        "}"
        ".even {"
        "background-color: #ffffff;"
        "}"
        "table {"
        "width: 100%;"
        "}"
        "th, td {"   /* *** \todo Fix. CSS th, td{} is messing up footer table alignment **** */
        "text-align: left;"
        "}"
        "</style>";

Changed to:

"<style type=\"text/css\">"
"@media print {"
".pagebreak-before:first-child { display: block; break-before: avoid; }"
".pagebreak-before { display: block; break-before: always; }"
"}"
".odd {"
"background-color: #efefef;"
"}"
".even {"
"background-color: #ffffff;"
"}"
"table {"
"width: 100%;"
"}"
"th, td {"   /* *** \todo Fix. CSS th, td{} is messing up footer table alignment **** */
"text-align: left;"
"}"
"</style>";