Anki – How to create a Deck from PDF flashcards

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:

  1. it extracts from the .pdf file the single pages as images;
  2. from each page it cuts all the cards along the prepared lines and saves each card in a single image file;
  3. 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.

  1. 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.
  2. Start Anki and create a new empty Deck.
  3. Select File->Import function from menu, “Text separated by tabs or semicolons” as type and choose the text file above created by pdf2anki script.
  4. 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.
  5. 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
  6. Copy in this subdirectory all the .png images created by pdf2anki script.
  7. 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.

This entry was posted in Anki. Bookmark the permalink.

6 Responses to Anki – How to create a Deck from PDF flashcards

  1. Peter says:

    I enjoy, cause I discovered exactly what I was taking a look for.
    You have ended my 4 day long hunt! God Bless you man.

    Have a great day. Bye

  2. Brian says:

    So I am looking to create an anki flashcard deck using some flashcard *.pdfs that I currently have (I actually would using any software that would allow me to study these pdfs as flashcards at random). I have no idea about code or programming. Can you help me out with this at all?

  3. Mags says:

    Please make a video tutorial of this using Windows please 🙁 I’m not computer science graduate.

  4. Vinny says:

    Hi. This is too complicated for me to understand, but this is exactly what I am trying to do for 3 pdf’s each containing 1,000 sentences in Spanish, their translations and IPA (international phonetic alphabet) transcriptions. So far I have been manually copying and pasting each sentence, translation, and IPA into my Anki flashcard template. It’s getting VERY tedious and I know if knew a little bit about python I could do all of this automatically instead of spending 30+ hours copying and pasting. Would you mind helping me out with this?

  5. Tina says:

    Hi,
    Thanks a lot for this! I was able to create the different images from the pdf, but I am not able to correctly add them to Anki. The pictures dont show, I just have the image symbol. Do you know why?
    Thanks

  6. KnightOfOne says:

    Just wanted to say thank you for this. I had to do a lot of trial and error to get this to work but it was worth it. I made a guide, on reddit, based off your script for people with no coding experience. Hope that’s okay.

    Here’s the link: https://www.reddit.com/r/Anki/comments/856n14/how_to_convert_pdf_flashcards_into_a_deck_on_anki/

Leave a Reply to Brian Cancel reply

Your email address will not be published. Required fields are marked *