sioyek icon indicating copy to clipboard operation
sioyek copied to clipboard

any way to enable "embed_annotations" on current file without exporting

Open Aneeqasif opened this issue 2 years ago • 5 comments

i was really getting annoyed by annotation disappearing on my colleague's pc but its showing on mine then i search and found this is not an option by default, and i landed on this https://github.com/ahrm/sioyek/issues/120#issuecomment-1042901191 but only things that is missing and is also mentioned in that issue is adding some option to do it on current file like, i do some highlights , i run embed_annotations_current and it just embed and annotate my current file without wasting time on handling a pop up to go to required location and then do an overwrite . sir i totally respect your opinion on not modifying pdfs but many people have different use cases and they do rely on annotating/permanently modifying their pdf . it would be too awesome if use have the choice whether to export to another file or modify current file for embed_annotations command while still preserving the highlights info in database Thanks :)

Aneeqasif avatar Aug 08 '22 15:08 Aneeqasif

I added a script that does that here. You also need to put the sioyek.py file next to it which provides the utility functions. Here is how to looks:

https://user-images.githubusercontent.com/6392321/183625416-774cdae3-2b3d-478a-b86e-6668e247477d.mp4

Here is how to configure it in prefs_user.config:

new_command _embed_annotations_in_file python /path/to/embed_annotations_in_file.py "%{sioyek_path}" "%{local_database}" "%{shared_database}" "%{file_path}"

Now every time you run the _embed_annotations_in_file command, we find the new sioyek annotations which are not present in the PDF file and embed them in the file. You can also bind this command to a key (in keys_user.config) so that it is easier to execute:

_embed_annotations_in_file <some key binding>

A few notes about the implementation: I had two choices on how to implement this. The easier way would be to delete all PDF annotations every time the command is executed and re-insert all sioyek annotations. The advantage of this method is that it is simpler and the PDF and sioyek annotations will always be in sync. However, if there exists other (non-sioyek) annotations in the PDF file they will be deleted. I thought this probably will be a huge problem for your workflow so I chose the other option: we enumerate all sioyek annotations and check if they exist in PDF file. If an annotation is present in sioyek but not in the file, we embed that annotation in the file. The advantage of this method is that we never delete anything so there is no danger of data loss. However, this method can not delete annotations (because we can not distinguish between deleted sioyek annotations and annotations that were created by others in other PDF software). So if you absolutely need to delete annotations you have to do it in other software.

ahrm avatar Aug 09 '22 10:08 ahrm

Also note that these scripts require newer version of sioyek and don't work with 1.4 release, I am building an experimental build of latest commit. I will post it here once it is ready.

ahrm avatar Aug 09 '22 10:08 ahrm

Updated in a1239ad339454ad9d15a300432a32dc77830f15b, we now handle highlight colors.

ahrm avatar Aug 09 '22 10:08 ahrm

Builds of the newerst commits are available here: https://github.com/hexomancer/sioyek/releases/tag/v0.31.365

ahrm avatar Aug 09 '22 11:08 ahrm

thats is awesome i really really appericaite your work for this feature, now i can totally shift to this software without any worry , one suggest (that can be totally dumb though) is as sioyek has a pluggin system kinnda, it might be a good idea to improve that system and then add such functionalities as plugins that anyone can add or remove by some simple steps

Aneeqasif avatar Aug 10 '22 21:08 Aneeqasif

awsome!

wenbopeng avatar Aug 14 '22 23:08 wenbopeng

_embed_annotations_in_file not working as of Sioyek 1.5 on Windows, how can I debug?

jleaves avatar Sep 28 '22 06:09 jleaves

Have you installed the python extensions? Maybe run this command:

python /path/to/embed_annotations_in_file.py "%{sioyek_path}" "%{local_database}" "%{shared_database}" "%{file_path}"

(after replacing the variables with the correct values of course) manually on command line and see if there are any errors.

ahrm avatar Sep 28 '22 09:09 ahrm

Have you installed the python extensions? Maybe run this command:

python /path/to/embed_annotations_in_file.py "%{sioyek_path}" "%{local_database}" "%{shared_database}" "%{file_path}"

(after replacing the variables with the correct values of course) manually on command line and see if there are any errors.

Thank you for the reply. Below is my result. There seems to be a missing module.

Traceback (most recent call last):
  File "C:\path\to\sioyek\embed_annotations_in_file.py", line 2, in <module>
    from sioyek import Sioyek
  File "C:\path\to\sioyek\sioyek.py", line 5, in <module>
    import fitz
ModuleNotFoundError: No module named 'fitz'

I'm using the portable Windows version. Could you tell me where i can find the module named 'fitz'? @ahrm

jleaves avatar Oct 06 '22 08:10 jleaves

You need to install PyMuPDF: https://pypi.org/project/PyMuPDF/

ahrm avatar Oct 06 '22 08:10 ahrm

Might be useful for others with the same problem. It turns out that you need to install these two packages apart from standard Python libraries:

jleaves avatar Oct 06 '22 10:10 jleaves

How did you install python extensions? these should be automatically installed when you install sioyek python extensions using pip because we have them in dependencies:

https://github.com/ahrm/sioyek-python-extensions/blob/7065663c4001bb862b3c74db01e94b7ae951ad93/pyproject.toml#L20

ahrm avatar Oct 06 '22 11:10 ahrm

Ahh, I didn't know there is a separate Python library called 'sioyek', which implements the "Python extensions". All I did was downloading the two .py files you mentioned earlier in this issue.

Now I see ..

jleaves avatar Oct 06 '22 11:10 jleaves

Everything now works! Thanks! The annotations are indeed embedded in the file itself.

There is one warning though (the warning can be seen if I run it from the command line):

Warning: fill color ignored for annot type 'Highlight'.

jleaves avatar Oct 06 '22 11:10 jleaves

Here is how to configure it in prefs_user.config:

new_command _embed_annotations_in_file python /path/to/embed_annotations_in_file.py "%{sioyek_path}" "%{local_database}" "%{shared_database}" "%{file_path}"

With the aforementioned line, I got

QString::arg: Argument missing: python /path/to/embed_annotations_in_file.py "%{sioyek_path}" "%{local_database}" "%{shared_database}" "%{file_path}", /path/to/pdf.pdf
QString::arg: Argument missing: python3 /path/to/embed_annotations_in_file.py "%{sioyek_path}" "%{local_database}" "%{shared_database}" "%{file_path}", /path/to/pdf.pdf
Traceback (most recent call last):
  File "/path/to/embed_annotations_in_file.py", line 38, in <module>
    sioyek = Sioyek(SIOYEK_PATH, LOCAL_DATABASE_PATH, SHARED_DATABASE_PATH)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/sioyek.py", line 44, in __init__
    self.local_database = sqlite3.connect(self.local_database_path)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sqlite3.OperationalError: unable to open database file

On Debian 12 Bookworm, package: sioyek/stable,now 2.0.0+dfsg-3+b2 amd64

I have solved it eliminating the quotes inside prefs_user.config, inserting this line instead:

new_command _embed_annotations_in_file python3 /path/to/embed_annotations_in_file.py %{sioyek_path} %{local_database} %{shared_database} %{file_path}

Dunno if it's an issue, but hope it is helpful to anyone.

Thanks for the work!!

CILT avatar Aug 04 '23 02:08 CILT