简体   繁体   中英

How to zip binary files during function’s run in eXist-db and xQuery

I have no problems with producing pdfs and offering them as response:stream-binary files for download. However, I have problems if I try to zip produced pdfs and do the same with zip. It offers the zip result for download. The pdf is included but has no size (empty file).

let $pdf-binary :=
(
    if ($pdfQuality eq 'proof' and $template eq 'draft')
        then xslfo:render(transform:transform($doc, $stylesProof, ()), 'application/pdf', (), $proofConf)
    else if ($pdfQuality eq 'print' and $template eq 'draft')
        then xslfo:render(transform:transform($doc, $stylesPrint, ()), 'application/pdf', (), $printConf)
    else if ($pdfQuality eq 'print' and $template eq 'auc-geographica')
        then xslfo:render(transform:transform($doc, $stylesAUCGeographica, ()), 'application/pdf', (), $printConf)
    else ()
)
return
    if (not($zipAll))
        then response:stream-binary($pdf-binary, 'application/pdf', $name  || '.pdf')
    else if ($zipAll)
        then (
            let $entry := <entry name="{$name}.pdf" type="binary" method="store">{util:binary-doc($pdf-binary)}</entry>
            let $zip-file := compression:zip($entry, false())
            return
                response:stream-binary($zip-file, 'application/zip', 'test.zip')
        )
    else ()

Important is I don't want to store pdf results anywhere in the DB.

Done. It was better to slightly rearrange the project. I used the whole logic in the query calling the rendering function. The working result is:

...
let $zip as item() := 
    (
        let $entries as item()+ := 
            (
                for $article at $count in $doc/article//tei:TEI
                let $year as xs:string := $article//tei:date/string()
                let $issue as xs:string := $article//tei:biblScope/string()
                let $pdf as xs:base64Binary := fop:render-pdf($article, $template, $name, $pdfQuality)
                return
                    <entry name="{lower-case($name)}-{$year}-{$issue}-{$count}.pdf" type="binary" method="store">{$pdf}</entry>
            )
            return
                compression:zip($entries, false())
    )
return 
       response:stream-binary($zip, 'application/zip', $name  || '.zip')

I would be very happy if anyone could revise this solution. I have learned it is really great idea to use strong typing and don't rely on automatic conversions. Without that this code does not work. Thanks a lot to the book about eXist-db, by Erik Siegel and Adam Retter, where I have seen a hint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM