4

I am trying to download and save a PDF but it fails while writing with an EOF-Error. What would be the correct way of doing this?

(with-open-file (file "/home/*/test.pdf"
                      :direction :io
                      :if-does-not-exist :create
                      :if-exists :supersede
                      :element-type '(unsigned-byte 8))
  (let ((input (drakma:http-request "http://www.fractalconcept.com/ex.pdf"
                                    :want-stream t)))
    (awhile (read-byte input)
      (write-byte it file))
    (close input)))
Sim
  • 4,199
  • 4
  • 39
  • 77

3 Answers3

8

The solution was that I forgot to use the two optional parameters of read-byte.

The correct way would be to set eof-error-p and eof-value to nil:

(with-open-file (file "/home/*/test.pdf"
                      :direction :output
                      :if-does-not-exist :create
                      :if-exists :supersede
                      :element-type '(unsigned-byte 8))
  (let ((input (drakma:http-request "http://www.fractalconcept.com/ex.pdf"
                                    :want-stream t)))
    (awhile (read-byte input nil nil)
      (write-byte it file))
    (close input)))
Sim
  • 4,199
  • 4
  • 39
  • 77
2
(ql:quickload "trivial-download")
(trivial-download:download URL FILE)

; Does pretty much exactly what your code does but in bigger chunks and can show a progress bar.

; QuickLisp can be found at http://QuickLisp.org.

Devon
  • 1,019
  • 9
  • 20
1

AWHILE is found in package ARNESI.

sebyte
  • 36
  • 1