• OKMDocument -> getContent

  • We tried to make OpenKM as intuitive as possible, but an advice is always welcome.
We tried to make OpenKM as intuitive as possible, but an advice is always welcome.
Forum rules: Please, before asking something see the documentation wiki or use the search feature of the forum. And remember we don't have a crystal ball or mental readers, so if you post about an issue tell us which OpenKM are you using and also the browser and operating system version. For more info read How to Report Bugs Effectively.
 #21281  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.
 #21290  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.";
?>
Last edited by olsonea on Fri Feb 08, 2013 11:02 pm, edited 1 time in total.
 #21291  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));
?>
 #21301  by jllort
 
I would like to share this code in our online documentation at wiki. Tell me if you're agree with it.
 #21310  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.

About Us

OpenKM is part of the management software. A management software is a program that facilitates the accomplishment of administrative tasks. OpenKM is a document management system that allows you to manage business content and workflow in a more efficient way. Document managers guarantee data protection by establishing information security for business content.