Create Anki Decks from PDF Flashcards
On GlyphStudy website you can find a lot of flashcards files in pdf format to help you to study Ancient Egyptian.
If you want to use them you should print them on a paper sheet (on both sides) and cut along the lines to produce a set of flashcards you can bring with you everywhere.
But we spend a lot of time in front of a computer screen so sometimes it could be nice to use flashcards on your computer.
Among all available flashcard programs probably the best is Anki.
I wanted to produce some Anki flashcard decks without starting from scratch since some other people already did such a good job and were so kind to share them with all the group.
I started with the files prepared by Bob Skinner for Collier&Manley Group.
I wrote a little script that using some utilities available on Linux to manipulate PDFs (pdftk – PDF Tool Kit) and images (convert – ImageMagick) basically works as follow:
- it extracts from the .pdf file the single pages as images;
- from each page it cuts all the cards along the prepared lines and saves each card in a single image file;
- it writes a text file where in each row mates the right front/back pairs.
The script is a shell script, I named it pdf2anki :
#!/bin/bash echo "Syntax: wo nome_file.pdf prefix" echo "Working on pdf: "$1 nome=$2_anki.txt immagini=$2_*.png rm $nome rm $immagini # width of each card let w=343 # height of each card let h=191 # number of columns starting from 0 (col=1 means two columns) let col=1 # number of rows starting from 0 (row=4 means five rows) let row=4 # discover pages number NPAGES=`pdftk $1 dumpdata | grep NumberOfPages | cut -d" " -f2` let pag=(NPAGES/2) let ciclo=pag-1 echo "Pagine con cards: "$pag # convert pdf pages to images convert -quality 100 -density 100x100 $1 $2_img_%d.png # working on each page for i in `seq 0 1 $ciclo`; do let b=i+pag echo "Working on page: "$i" e "$b imgf=$2"_img_"$i".png" imgb=$2"_img_"$b".png" # border offsets let floff=103 # front pages - X left top border offset let ftoff=53 # front pages - Y left top border offset let bloff=60 # back pages - X left top border offset let btoff=53 # back pages - Y left top border offset for r in `seq 0 1 $row`; do for c in `seq 0 1 $col`; do let x=floff+w*c+c*3+6 let y=ftoff+h*r+r*2 fcarg=$w"x"$h"+"$x"+"$y outf=$2"_"$i$r$c"f.png" if [ $c -eq 0 ] ; then bc=1 else bc=0 fi let x=bloff+w*c+c*2 let y=btoff+h*r+r*2 bcarg=$w"x"$h"+"$x"+"$y outb=$2"_"$i$r$bc"b.png" #echo $imgf #echo $carg echo $outf - $outb convert $imgf -crop $fcarg $outf convert $imgb -crop $bcarg $outb done done done # write text file for Anki for i in `seq 0 1 $ciclo`; do for r in `seq 0 1 $row`; do for c in `seq 0 1 $col`; do outf=$2"_"$i$r$c"f.png" outb=$2"_"$i$r$c"b.png" echo "<img src=\""$outf"\">;<img src=\""$outb"\">">>$nome done done done
It can be invoked in this way:
pdf2anki <pdf_file> <prefix>
where <pdf_file> is the name of .pdf file containing the flashcards and <prefix> will be used as prefix for all names of produced image files.
Note: the script must be targeted for every flashcard source format (may be you have to do some tries to find right values to use in the script) sure you must set right number of columns and rows (col and row), width and height of each flashcard (w and h) and offsets of top left border for front (floff and ftoff) and back pages (bloff and btoff).
Once completed you’ll have a lot of .png images, two for each flashcard (the front and the back side) named using the following convention for each pair: <prefix><flashcard _number>f.png for the front side and <prefix><flashcard _number>b.png for the back side.
The script also produces a text file named <prefix>_anki.txt ready to be imported in Anki with a content like this:
<img src="ch1voc_000f.png">;<img src="ch1voc_000b.png"> <img src="ch1voc_001f.png">;<img src="ch1voc_001b.png"> <img src="ch1voc_010f.png">;<img src="ch1voc_010b.png"> <img src="ch1voc_011f.png">;<img src="ch1voc_011b.png"> <img src="ch1voc_020f.png">;<img src="ch1voc_020b.png"> <img src="ch1voc_021f.png">;<img src="ch1voc_021b.png"> <img src="ch1voc_030f.png">;<img src="ch1voc_030b.png"> <img src="ch1voc_031f.png">;<img src="ch1voc_031b.png"> <img src="ch1voc_040f.png">;<img src="ch1voc_040b.png"> <img src="ch1voc_041f.png">;<img src="ch1voc_041b.png"> <img src="ch1voc_100f.png">;<img src="ch1voc_100b.png" ...
Now everything is ready to create your Anki Deck.
- First check if pages in the source .pdf file are not full and there are some empty cards to fill the pages since the script will consequently produce some blank images. If there are any delete them and also delete the corresponding lines in the text file above.
- Start Anki and create a new empty Deck.
- Select File->Import function from menu, “Text separated by tabs or semicolons” as type and choose the text file above created by pdf2anki script.
- When finished don’t try the deck, it will not work yet. Save it, giving it the name you want, the text you used as <prefix> could be a good choice.
- Go to the directory where you saved your deck and create a subdirectory, assuming the name of your Anki deck file is <name>.anki, the name of the subdirectory should be <name>.media
- Copy in this subdirectory all the .png images created by pdf2anki script.
- Start Anki again and practice with your deck, everything should work fine now.
If you think this is too much complicated or simply just don’t have time to play with all the stuff above just go to this page and download the Collier&Manley Anki Decks already prepared.