Modify a PDF to link to local audio-visual files using Python tools
A bookle is a multimedia virutal book, readable on your own off-line computer using server technology. Reading a bookle is like reading a PDF but with links redirected to locally stored audio-visual files instead of resources on the web, allowing you to read the bookle offline. The instructions below describe how to create a bookle on your computer.
- Install Python3, perhaps the easiest way to install a server.
- Create a folder named
server
, for example. - Inside the folder, create an executable file to launch the server. On a Windows system, this could be a batch file such as
server.bat
containing the single line:python -m http.server 80
. This file can be placed outside theserver
folder if the commandcd path-to-server
is inserted at its beginning. Wherever the Python server is invoked becomes the server's document root folder. - Click on
server.bat
to start the server. - On Windows, a warning about your computer's firewall will appear. You should enable private network access and disable public network access. This only needs to be done once. On other platforms, follow the appropraite procedure to open your firewall for the Python server.
- A shell window opens and stays open to log server activity. Closing the shell window stops the Python server.
- Test the server by opening a browser and typing
localhost
in the URL field. A directory of the server folder is shown.
- Download the code from this repository into a separate folder named
bookle
, for example. - There are two Python programs,
extract.py
andreplace.py
, which can be started from a shell window. - Each program takes one argument, the pathname of the PDF file to be converted.
- The batch command files named
extract.bat
andreplace.bat
are provided to automatically launch these Python programs for those not familiar with running Python programs from a shell window. - The first program,
extract.py
, creates a file namedurls.txt
with one line for each URL in the specified PDF file. Usage:python extract.py file.pdf
, or simply launchextract.bat
. - The second program,
replace.py
, readsurls.txt
after it has been modified, using it to replace URLs in the specified PDF file. Usage:python replace.py file.pdf
, or simply launchreplace.bat
. - There is also a third program,
server.bat
, mentioned in Step 1.3.
- Move any audio-visual files you are serving locally into the server folder created in Step 1.2, preferably into a subfolder in case you have more than one bookle. These files should be renamed to form proper URLs. I recommend using the character set a-z, A-Z, 0-9, period, underscore and hyphen, but you can also use urlencoder.org
- Extract the URLs from the PDF file as explained in Step 2.5.
- Edit the created
urls.txt
file, appending to each URL a comma followed by the pathname of an audio-visual file, relative to the server folder. - Example line in
urls.txt
(for an mp4 video file stored in theserver/fs
subfolder):https://youtu.be/Hrowi4hHz8A,fs/Yes-Love_Will_Find_a_Way.mp4
- If you do not have a local file for one of the URLs in
urls.txt
, either leave it as is or delete the line it is in. - Replace the URLs in the PDF file according to the modified
urls.txt
, as explained in Step 2.6. - Move the converted PDF file to the server folder created in Step 1.2.
- Test the bookle based on the converted PDF by:
a.) starting the server as explained in Step 1.4,
b.) entering
localhost
in the URL field of a browser, c.) clicking on the filename of your converted PDF file, d.) then after the PDF opens, clicking on a link to a local audio-visual file.
- Use a browser that can display PDFs and play audio-visual resources such as MP3s and MP4s within its window, such as the Chrome browser.
- When a link is clicked in a PDF, the target resource loads into the current tab. If you then return to the PDF, you may be at the beginning, having lost your place. To avoid this problem, instead hold down the control key when clicking a link to open the target in a new tab. Holding down both the shift and control keys when clicking shifts focus to the new tab immediately. Alternatively, you can press the mouse wheel (optionally with the shift key held down).
- You can download YouTube videos using the service ssyoutube. Other services are available for other video platforms that, like YouTube, do not already offer easy downloading.
- Wikipedia articles can easily be downloaded as PDFs using the Tools menu righ on the Wikipedia article page.
- Any web page can be downloaded as a PDF using a service such as Web Page to PDF