dennismonsewicz Posted January 29, 2010 Share Posted January 29, 2010 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? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted January 30, 2010 Share Posted January 30, 2010 pdf_fit_image('test.pdf', $image, 0, 50, ""); It says argument one must be a resource and you're giving it a string. You probably want to use this instead: pdf_fit_image( $pdfdoc, $image, 0, 50, ""); Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted January 30, 2010 Author Share Posted January 30, 2010 hmmm I think I am confused... what is meant by resource? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted January 30, 2010 Share Posted January 30, 2010 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 Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted January 30, 2010 Author Share Posted January 30, 2010 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! Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted January 30, 2010 Share Posted January 30, 2010 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! Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted January 30, 2010 Author Share Posted January 30, 2010 Hmmm alright... I will keep looking then... I appreciate it buddy! Quote Link to comment Share on other sites More sharing options...
teamatomic Posted January 30, 2010 Share Posted January 30, 2010 maybe try: $pdf_file='/path/to/test.pdf'; $pdfdoc = pdf_new(); pdf_open_file($pdfdoc, $pdf_file); HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.