xquery version '3.1'; (:~ It requires the Pdfbox jars to be on the classpath, or a EXPath package (xar) installation.
style: Roman, Decimal etc, start: the index to start from (default 1) and prefix: an optional string to prefix to the page label e.g "Vol1:"$pdf to filesystem at $savepath , returns $savepath :)
declare function pdfbox:pdf-save($pdf as item(),$savepath as xs:string)
as xs:string{
PDDocument:save($pdf, File:new($savepath)),$savepath
};
(:~ Create binary representation (xs:base64Binary) of $pdf object
@param $pdf pdf object, created by pdfbox:open
@see #pdfbox:open
@see #pdfbox:with-pdf
:)
declare function pdfbox:binary($pdf as item())
as xs:base64Binary{
let $bytes:=Q{java:java.io.ByteArrayOutputStream}new()
let $_:=PDDocument:save($pdf, $bytes)
return Q{java:java.io.ByteArrayOutputStream}toByteArray($bytes)
=>convert:integers-to-base64()
};
(:~ Release any resources related to $pdf:)
declare function pdfbox:close($pdf as item())
as empty-sequence(){
(# db:wrapjava void #) {
PDDocument:close($pdf)
}
};
(:~ Number of pages in PDF:)
declare function pdfbox:number-of-pages($pdf as item())
as xs:integer{
PDDocument:getNumberOfPages($pdf)
};
(:~ Pdf page as image (zero is cover)
options.format="bmp jpg png gif" etc, options.scale= 1 is 72 dpi?? :)
declare function pdfbox:page-render($pdf as item(),$pageNo as xs:integer,$options as map(*))
as xs:base64Binary{
let $options := map:merge(($options,map{"format":"jpg","scale":1}))
let $bufferedImage := PDFRenderer:new($pdf)
=>PDFRenderer:renderImage($pageNo,$options?scale)
let $bytes := Q{java:java.io.ByteArrayOutputStream}new()
let $_ := Q{java:javax.imageio.ImageIO}write($bufferedImage ,$options?format, $bytes)
return Q{java:java.io.ByteArrayOutputStream}toByteArray($bytes)
=>convert:integers-to-base64()
};
(:~ Defines a map from property names to evaluation method.
Keys are property names,
values are sequences of functions to get property value starting from a $pdf object.
:)
declare %private variable $pdfbox:property-map:=map{
"#pages": pdfbox:number-of-pages#1,
"#bookmarks": pdfbox:number-of-bookmarks#1,
"#labels": pdfbox:number-of-labels#1,
"specification":pdfbox:specification#1,
"title": (PDDocument:getDocumentInformation#1,
PDDocumentInformation:getTitle#1) ,
"author": (PDDocument:getDocumentInformation#1,
PDDocumentInformation:getAuthor#1 ),
"creator": (PDDocument:getDocumentInformation#1,
PDDocumentInformation:getCreator#1),
"producer": (PDDocument:getDocumentInformation#1,
PDDocumentInformation:getProducer#1),
"subject": (PDDocument:getDocumentInformation#1,
PDDocumentInformation:getSubject#1),
"keywords": (PDDocument:getDocumentInformation#1,
PDDocumentInformation:getKeywords#1),
"creationDate": (PDDocument:getDocumentInformation#1,
PDDocumentInformation:getCreationDate#1,
pdfbox:gregToISO#1),
"modificationDate": (PDDocument:getDocumentInformation#1,
PDDocumentInformation:getModificationDate#1,
pdfbox:gregToISO#1),
"labels": pdfbox:labels-as-string#1
};
(:~ Defined property names, sorted :)
declare function pdfbox:property-names()
as xs:string*{
$pdfbox:property-map=>map:keys()=>sort()
};
(:~ Return the value of $property for $pdf :)
declare function pdfbox:property($pdf as item(),$property as xs:string)
as item()*{
let $fns:= $pdfbox:property-map($property)
return if(exists($fns))
then fold-left($fns,
$pdf,
function($result,$this as function(*)){$result!$this(.)})
else error(xs:QName('pdfbox:property'),concat("Property '",$property,"' not defined."))
};
(:~ summary CSV style info for named $properties for PDFs in $pdfpaths
@see https://docs.basex.org/main/CSV_Functions#xquery
:)
declare function pdfbox:report($pdfpaths as item()*, $properties as xs:string*)
as map(*){
map{"names": array{"path",$properties},
"records": for $path in $pdfpaths
let $name:=if($path instance of xs:base64Binary) then "binary" else $path
return try{
let $pdf:=pdfbox:open($path)
return (fold-left($properties,
array{$name},
function($result as array(*),$prop as xs:string){
array:append($result, string(pdfbox:property($pdf, $prop)))}
), pdfbox:close($pdf)
)
} catch *{
fold-left($properties,
array{$name},
function($result as array(*),$prop as xs:string){
array:append($result, "#ERROR")}
)
}
}
};
(:~ summary CSV style info for all properties for $pdfpaths
:)
declare function pdfbox:report($pdfpaths as xs:string*)
as map(*){
pdfbox:report($pdfpaths,pdfbox:property-names())
};
(:~ Convenience function to save report() data to file :)
declare function pdfbox:report-save($data as map(*),$dest as xs:string)
as empty-sequence(){
let $opts := map { "format":"xquery", "header":"yes", "separator" : "," }
return file:write-text($dest,csv:serialize($data,$opts))
};
(:~ The number of outline items defined in $pdf :)
declare function pdfbox:number-of-bookmarks($pdf as item())
as xs:integer{
let $xml:=pdfbox:outline-xml($pdf)
return count($xml//bookmark)
};
(:~ XMP metadata as "RDF" document
@note usually rdf:RDF root, but sometimes x:xmpmeta
:)
declare function pdfbox:metadata($pdf as item())
as document-node(element(*))?
{
let $m:=PDDocument:getDocumentCatalog($pdf)
=>PDDocumentCatalog:getMetadata()
return if(exists($m))
then
let $is:=PDMetadata:exportXMPMetadata($m)
return pdfbox:do-until(
map{"n":0,"data":""},
function($input,$pos ) { pdfbox:read-stream($is,$input?data)},
function($output,$pos) { $output?n eq -1 }
)?data=>parse-xml()
else ()
};
(:~ read next block from XMP stream :)
declare %private function pdfbox:read-stream($is,$read as xs:string)
as map(*){
let $blen:=4096
let $buff:=Q{java:java.util.Arrays}copyOf(array{xs:byte(0)},$blen)
let $n:= COSInputStream:read($is,$buff,xs:int(0),xs:int($blen))
let $data:=convert:integers-to-base64(subsequence($buff,1,$n))=>convert:binary-to-string()
return map{"n":$n, "data": $read || $data}
};
(:~ Return outline for $pdf as map()* :)
declare function pdfbox:outline($pdf as item())
as map(*)*{
(# db:wrapjava some #) {
let $outline:=
PDDocument:getDocumentCatalog($pdf)
=>PDDocumentCatalog:getDocumentOutline()
return if(exists($outline))
then pdfbox:outline($pdf,PDOutlineItem:getFirstChild($outline))
else ()
}
};
(:~ return bookmark info for children of $outlineItem as seq of maps :)
declare %private function pdfbox:outline($pdf as item(),$outlineItem as item()?)
as map(*)*{
let $find as map(*):=pdfbox:outline_($pdf ,$outlineItem)
return map:get($find,"list")
};
(:~ outline helper. BaseX bug 10.7? error if inlined in outline :)
declare %private function pdfbox:outline_($pdf as item(),$outlineItem as item()?)
as map(*){
pdfbox:do-until(
map{"list":(),"this":$outlineItem},
function($input,$pos ) {
let $bookmark:=$input?this
let $bk:=map{
"index": PDOutlineItem:findDestinationPage($bookmark,$pdf)=>pdfbox:find-page($pdf),
"title": (# db:checkstrings #) {PDOutlineItem:getTitle($bookmark)}
}
let $bk:= if(PDOutlineItem:hasChildren($bookmark))
then let $kids:=pdfbox:outline($pdf,PDOutlineItem:getFirstChild($bookmark))
return map:merge(($bk,map:entry("children",$kids)))
else $bk
return map{
"list": ($input?list, $bk),
"this": PDOutlineItem:getNextSibling($input?this)}
},
function($output,$pos) { empty($output?this) }
)
};
(:~ PDF outline in xml format :)
declare function pdfbox:outline-xml($pdf as item())
as element(outline)?{
let $outline:=pdfbox:outline($pdf)
return if(exists($outline))
then