Autoreload fails when pdf is not ready yet (e.g. being build using latex)
I wanted to use termpdf for automatic reloading when writing latex code. The problem is that everything stops working after changing something in the tex file and compiling -- The program quits and also the text in my terminal becomes invisible (I think this is just a side effect termpdf terminating unexpectedly)
The issue seems to be that termpdf.py reloads the pdf too quickly, even though it is not available yet. After changing
doc_viewer = threading.Thread(target=view, args=(file_change, doc))
doc_viewer.start()
to
view(file_change, doc)
I get the following error message
Traceback (most recent call last):
File "<PATH>/termpdf.py/termpdf.py", line 1908, in <module>
main()
File "<PATH>/termpdf.py/termpdf.py", line 1901, in main
view(file_change, doc)
File "<PATH>/termpdf.py/termpdf.py", line 1546, in view
bar.update(doc)
File "<PATH>/termpdf.py/termpdf.py", line 1033, in update
p = doc.page_to_logical()
^^^^^^^^^^^^^^^^^^^^^
File "<PATH>//termpdf.py/termpdf.py", line 465, in page_to_logical
return self.logical_pages[p]
~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range
I printed out doc.dict and after the change in the pdf it becomes
{'is_closed': False, 'is_encrypted': False, 'metadata': {'format': 'PDF 1.5', 'title': '', 'author': '', 'subject': '', 'keywords': '', 'creator': '', 'producer': '', 'creationDate': '', 'modDate': '', 'trapped': '', 'encryption': None}, 'FontInfos': [], 'Graftmaps': {}, 'ShownPages': {}, 'InsertedImages': {}, '_page_refs': <WeakValueDictionary at 0x10bf92900>, 'stream': None, '_name': '<path to my pdf>', 'thisown': True, '_graft_id': 2, '_outline': <pymupdf.Outline object at 0x10dcdf640>, 'filename': 'Documents/work/promotion/progress-reports/latex/build/2024-07-03.pdf', 'citekey': None, 'papersize': 3, 'page': 0, 'logicalpage': '11', 'prevpage': 0,
'pages': -1, 'first_page_offset': 1, 'logical_pages': [], 'chapter': 0, 'rotation': 0, 'fontsize': 12, 'width': 0, 'height': 0, 'autocrop': False, 'manualcrop': False, 'manualcroprect': [None, None], 'alpha': False, 'invert': False, 'tint': False, 'tint_color': 'antiquewhite2', 'nvim': None, 'nvim_listen_address': '/tmp/termpdf_nvim_bridge', 'page_states': []}
So the program loads a file with zero pages (doc.logical_pages = []) but cannot handle this.
To Reproduce
- Open resulting pdf of Latex file with termpdf
- change something in file source code and compile (e.g. with latexmk)
I think ideally termpdf.py should have some mechanism of trying to reload the pdf and entering a retry loop on encountering this error and keep showing the old pdf till this issue is resolved,
My workaround is to copy or move the build pdf file to a separate file after the build completed and display that file. As I work with Makefiles for building my pdf the workaround only has to be set up once working just fine. But still termpdf implementing this would be easier