Downloading and Appending PDFs
Question: I'm trying to download all the pdf urls from a website and append all the pdfs
into a single file. At the moment, I have a list of all the urls containing
pdfs. How can I download all the pdfs and append them together? I've attached
my code below. I'm using Python 2.7.8.

    
    
    # Download and merge pdfs
    url_list = listofurl
    for url in listofurl:
        outfile = os.path.basename(url)
        with open(outfile, 'w') as out:
            out.write(urllib2.urlopen(url).read())
    


Answer: For me the download works but it throws an exception at one point where a file
is not found

    
    
    HTTPError: HTTP Error 404: Not Found
    

I am not sure if python itself is able to merge the files. I would recommend
using "pdftk" and call it via the 'subprocess' module once your files are on
your harddisk.

On a linux system it works like this once 'pdftk' (an external and very
practical pdf merger for the command line) is installed:

    
    
    from subprocess import call
    
    call(['pdftk', '*.pdf', 'cat', 'output', 'combined.pdf'])
    

It is not quite the most pythonic way but the easiest I can think of at the
moment. Hope it helps.

