Page 1 of 1
OKMDocument -> getContent
PostPosted:Fri Feb 08, 2013 2:24 am
by olsonea
I've got some documents in OKM that I want to make available on a custom web page. I want to use the getContent method to allow a user to save the document locallaly to their computer. I am able to list documents in a folder via PHP, but I can't figure out how to write the byte array returned by getContent to disk. Has anyone done this before that can point me in the right direction? Can I do this with PHP, or do I need to write the file with a client side language, like Javascript? Thanks.
Re: OKMDocument -> getContent
PostPosted:Fri Feb 08, 2013 10:40 pm
by olsonea
I figured out the fwrite is expecting a string and not a byte array, so I'm trying to convert the results of getContent to a string. I think I'm almost there, but could use a shove in the right direction.
Here's my code:
Code: Select all<?php
// Register WSDL
$OKMAuth = new SoapClient('http://OpenKM:8080/OpenKM/services/OKMAuth?wsdl');
$OKMDocument = new SoapClient('http://OpenKM:8080/OpenKM/services/OKMDocument?wsdl');
$OKMFolder = new SoapClient('http://OpenKM:8080/OpenKM/services/OKMFolder?wsdl');
$path = '/okm:root/Customer Docs/olsonea@gmail.com';
// Login
$loginResp = $OKMAuth->login(array('user' => 'okmAdmin', 'password' => 'admin'));
$token = $loginResp->return;
echo "Token: ".$token."<br>";
//open document test
$docPath = '/okm:root/Customer Docs/foo/Invoice-0000034.pdf';
echo "Document: ".$docPath."<br>";
//get document content
$getDocumentResp = $OKMDocument->getContent(array('token' => $token, 'docPath' => $docPath, checkout => 0));
echo "Binary Length: ".count($getDocumentResp)."<br>";
echo "Binary Data:<br>".$getDocumentResp->return."<br><br>";
//convert output from byte[] to string
//$bytes = $getDocumentResp->return;
$docstring = call_user_func_array("pack", array_merge(array("C*"), $getDocumentResp->return));
echo "String Data:<br>".$docstring."<br>";
$strlen = strlen($docstring);
echo "String Length: ".$strlen."<br>";
//write output to disk
$fp = fopen('test.pdf','w');
$fwrite = fwrite($fp,$docstring,$strlen);
$fclose($fp);
//access the file and echo back the data
$data = file_get_contents('test.pdf');
echo "Written Data:<br>".$data."<br>";
// Logout
$OKMAuth->logout(array('token' => $token));
echo "Logged out.";
?>
Re: OKMDocument -> getContent
PostPosted:Fri Feb 08, 2013 11:02 pm
by olsonea
Just wanted to share a quasi-solution that I found. I was able to display the file in a browser window, and from there the user can save.
Code: Select all<?php
// Register WSDL
$OKMAuth = new SoapClient('http://OpenKM:8080/OpenKM/services/OKMAuth?wsdl');
$OKMDocument = new SoapClient('http://OpenKM:8080/OpenKM/services/OKMDocument?wsdl');
$OKMFolder = new SoapClient('http://OpenKM:8080/OpenKM/services/OKMFolder?wsdl');
// Login
$loginResp = $OKMAuth->login(array('user' => 'okmAdmin', 'password' => 'admin'));
$token = $loginResp->return;
//open document test
$docPath = '/okm:root/Customer Docs/foo/Invoice-0000034.pdf';
//get document content
$getDocumentResp = $OKMDocument->getContent(array('token' => $token, 'docPath' => $docPath, checkout => 0));
$length = strlen($getDocumentResp->return);
//display document response as pdf
header("Content-type: application/pdf");
header("Content-Length: $length");
header("Content-Disposition: inline; filename='test.pdf'");
echo $getDocumentResp->return;
// Logout
$OKMAuth->logout(array('token' => $token));
?>
Re: OKMDocument -> getContent
PostPosted:Sat Feb 09, 2013 9:59 am
by jllort
I would like to share this code in our online documentation at wiki. Tell me if you're agree with it.
Re: OKMDocument -> getContent
PostPosted:Sat Feb 09, 2013 12:07 pm
by pavila
Re: OKMDocument -> getContent
PostPosted:Sun Feb 10, 2013 12:15 am
by olsonea
jllort wrote:I would like to share this code in our online documentation at wiki. Tell me if you're agree with it.
Yes, I consent. Please share this on the wiki.
Re: OKMDocument -> getContent
PostPosted:Sun Feb 10, 2013 11:41 am
by jllort