DelphiWorlds / Kastri

Cross-platform library for Delphi
MIT License
486 stars 117 forks source link

PDFViewer on Android crashes for large files #125

Open GitUserMarkus opened 1 year ago

GitUserMarkus commented 1 year ago

I am using your PDFViewer as shown in your demo application. When loading a huge file (say 1000 pages) and scrolling through the file, the app eventually crashes (typically when reaching some point beyond page 100). No exception is thrown, even the debugger gets no notice of the problem. Any idea how to fix this issue?

DelphiWorlds commented 1 year ago

Did you test the same PDF with the demo (i.e. instead of it using Feature Matrix.pdf)? I just tested a 914 page file, and it loads the first 500 pages (I stopped scrolling after that) without crashing. If the demo crashes, are you able to provide a PDF file to test with?

GitUserMarkus commented 1 year ago

Thank you very much for testing. With the original demo the error does not occur so it must have to do with the modifications I made. However, since I only added the support to zoom into the pdf, which is not in action while scrolling, I currently do not understand which side effect might be responsible for the crash. Sorry for bothering.

DelphiWorlds commented 1 year ago

I had planned to add zooming, however I am yet to do that. I'll keep this issue open as a reminder :-)

GitUserMarkus commented 1 year ago

Great, looking forward see how you will implement it.

GitUserMarkus commented 1 year ago

Now I can reproduce the crash for your demo app with just one modification. I call TPDFRenderer.RenderPage with AScale = 5.0 which I originally did in order to have a high resolution bitmap for zooming in. With this single change your demo crashes as reported above.

DelphiWorlds commented 1 year ago

Thanks for the update! I'll look into it

DelphiWorlds commented 1 year ago

Still cannot get it to crash. Please show exactly what you are changing, and it may also help if I know which PDF file you are using

GitUserMarkus commented 1 year ago

This is the change I made (the last argument of FRenderer.RenderPage is set to 5.0, overwriting the default 1.0

procedure TPDFControl.LoadPage; var LPage: TPDFPage; begin Inc(FPageIndex); LPage := TPDFPage.Create(Self); LPage.PageIndex := FPageIndex; LPage.Height := FVertScrollBox.ClientHeight; LPage.Position.Y := FVertScrollBox.ContentBounds.Height; LPage.Align := TAlignLayout.Top; LPage.Parent := FVertScrollBox; {$IF Defined(ANDROID)} LPage.Image.Bitmap.SetSize(LPage.Image.Size.Size.Round); FRenderer.RenderPage(FPageIndex, LPage.Image.Bitmap, 5.0); {$ENDIF} end;

The crash happens on Android 64 phones (I am not aware if it can be observed in any simulator). I can't share the PDF I am using publically, if you provide any means for a personal communication I will send it to you.

DelphiWorlds commented 1 year ago

It was my bad.. I tested with scale of 0.5 instead of 5.0!

I've removed the scaling of the native bitmap (not sure why I did it that way!). I suspect the scaling should be done in the bitmap in the image in the control instead. Could you help work out how to do that?

GitUserMarkus commented 1 year ago

If I remember correctly my experiments, without scaling the native bitmap (giving it a higher resolution than 1), the bitmap in the control will have a resolution which is not good enough for zooming in, too. Then it would be necessary to somehow link a page in the control to the corresponding page of the pdf such that one could rerender the bitmap with the needed resolution while zooming. A workaround could be to use a high scale (say 5) on creation of the bitmap, but to keep only a subset of the pages within the control. This would save the memoty for many hight resolution bitmaps (which is what I assume the reason for crashing). However, so far I did not find the time to implement something like that myself.