Jump to content

Write to PDF and place image using PHP


dennismonsewicz

Recommended Posts

I am trying to create script that I can open a PDF file and write an image to this file.

 

Here is my code so far

function processPDF($img) {
			$imgFile = "images/" . $img . ".jpg";
			$fd = fopen("test.pdf", "w");
			 $pdfdoc = pdf_new();
			 pdf_open_file($pdfdoc, "test.pdf");
			 $image = pdf_load_image($pdfdoc, "jpeg", $imgFile, "");
			 pdf_fit_image('test.pdf', $image, 0, 50, "");
			 pdf_close_image($image);
			 fclose($fd);
		}

 

I have found several tutorials on how to create brand new PDF document, I already have a PDF doc I want to use. The error I am getting with the above is this:

 

Fatal error: Uncaught exception 'PDFlibException' with message 'pdf_fit_image() expects parameter 1 to be resource, string given' in /var/www/barcode/index.php:23 Stack trace: #0 /var/www/barcode/index.php(23): pdf_fit_image('test.pdf', 1, 0, 50, '') #1 /var/www/barcode/index.php(28): processPDF('hello') #2 {main} thrown in /var/www/barcode/index.php on line 23

 

Any ideas?

Link to comment
Share on other sites

Programming languages have data types: integer, string, boolean, float, double, etc.

 

resource is a data type in PHP.  Whenever you successfully connect to a database, open a file, a socket connection, etc. the data type returned is a resource.

 

I've never built PDF files with PHP so I can not know for sure (although you can if you read the documentation), but I would guess that the following lines:

$pdfdoc = pdf_new();
			 pdf_open_file($pdfdoc, "test.pdf");

effectively turn your variable $pdfdoc into a resource

 

The error message can not be any more clear (once you're used to seeing them and understand what they mean):

message 'pdf_fit_image() expects parameter 1 to be resource, string given

Link to comment
Share on other sites

ah gotcha... I follow you now... sorry for the newb question...

 

so I changed my code just a little bit:

function processPDF($img) {

			$imgFile = "images/" . $img . ".jpg";
			 $pdfdoc = pdf_new();
			 pdf_open_file($pdfdoc, "test.pdf");
			 $image = pdf_load_image($pdfdoc, "jpeg", $imgFile, "");
			 pdf_fit_image($pdfdoc, $image, 0, 50, "");
			 pdf_close($image);
		}

 

but now I am getting the following error:

Fatal error: Uncaught exception 'PDFlibException' with message 'Function must not be called in 'document' scope' in /var/www/barcode/index.php:31 Stack trace: #0 /var/www/barcode/index.php(31): pdf_fit_image(Resource id #4, 1, 0, 50, '') #1 /var/www/barcode/index.php(36): processPDF('hello') #2 {main} thrown in /var/www/barcode/index.php on line 31

 

And thanks for ALL of your help thus far!

Link to comment
Share on other sites

http://php.he.net/manual/en/ref.pdf.php

 

If you look at the functions, you see some named like:

pdf_begin_document()

pdf_begin_page()

 

I imagine that API segments a document into logical pieces much like an API for a web page would.  In other words, in an HTML document you have the head section, the body section, etc.  Well in the PDF document you have document scope, page scope, etc.  You have to set the library into page scope to call that function.

 

How to do that?  I don't know!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.