Read Pdf From Database (Psql Large Object)

Hello everybody!

I’m using a postgresql 9.1 database, where a pdf file is stored in a large object field (oid). Now I want to show the pdf-file in a link where the user can click for watching it. I’ve searched a long time for doing this with Yii but without success.

I wondered if Yii bringing such stuff with it?

Or do I have to use the php [font="Courier New"]pg_lo_open[/font] and [font="Courier New"]pg_lo_read[/font] function itself? If so how can I get the database connection ressource needed by this functions (which normally will be returned from pg_connect function)? I thought about


but this seems not the correct ressource.

Can anyone help me please?


Any ideas?

I found this tutorial and followed it.

In my model I added the following rule:

	public function rules()


		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('document', 'file', 'types' =>'pdf'),



In my controller I added the following action:

	public function actionShowDoc($id) {

		$model = Docs::model()->findByPk($id);

		$this->renderPartial('_pdf_test', array('model'=>$doc));


And I added a new view file _pdf_test.php where I put:


header('Conent-Type: PDF');

print $model->document;


But it displays only the large object id (for example 18631) which you can also see in database by sql statement.

Am I doing something wrong?


Hello again,

yesterday I tried to use a bytea instead of a large object type in my database and doing the stuff which I wrote above. This works fine if I put the result of


in the


function, reading here.

I can’t use this function for the large object type because in this case


returns not a resource. At this time I use following workaround:

Yii::app()->db->createCommand("select lo_export(docs.document, '/tmp/$model->name.pdf') FROM docs where document=$model->document")->execute();

This statement saves the large object as pdf into /tmp folder. Then I load and display it in pdf viewer.

Has anyone another idea?