Search code examples
marklogic

MarkLogic - How to save pdfs to Filesystem


I retrieve a list of pdf files from MarkLogic. I would like to save them to disk. I tried xdmp:save(), but could not get it working.

(:from query return:
       /pdf/2179661.pdf
       /pdf/1092813.pdf
:)

let $list := cts:uris((),(),cts:collection-query("/pdf"))
for $pdf in $list
let $file-name := fn:substring-after($pdf,"/pdf/")
   return xdmp:save(concat("/output-pdf/",$file-name),$pdf)

Returns this error:

Error: [1.0-ml] XDMP-ARGTYPE: (err:XPTY0004) xdmp:save("/output-pdf/2179661.pdf", "/pdf/2179661.pdf") -- arg2 is not of type node()


Solution

  • When using xdmp:save(), the second parameter is the node() that you want to save.

    In the code that you posted, you are passing in the URI of the PDF document, not the actual document-node().

    Either use fn:doc() to obtain the document:

    let $list-pdf := cts:uris((),(),cts:collection-query("/pdf")) 
    for $pdf in $list-pdf 
    let $file-name := fn:substring-after($pdf,"/pdf/") 
    return xdmp:save(concat("/output-pdf/",$file-name), fn:doc($pdf))
    

    Or you could use cts:search() instead and reference the fn:base-uri() of the document:

    let $list-pdf := cts:search(/,cts:collection-query("/pdf")) 
    for $pdf in $list-pdf 
    let $file-name := fn:substring-after($pdf/base-uri(), "/pdf/") 
    return xdmp:save(concat("/output-pdf/",$file-name), $pdf)