0

I use this often and would like to turn it into a function:

$f = fopen('../images/snotel/'. $name .'.pdf','w+');
fwrite($f, $pdf);
fclose($f);
$conv='/usr/bin/convert../images/snotel/'. $name .'.pdf ../images/snotel/'. $name .'.jpg'; 
system ($conv); 

This is what I've tried but it doesn't seem to work:

function pdf2jpg($name)
{
 $f = fopen('../images/snotel/'. $name .'.pdf','w+');
 fwrite($f, $pdf);
 fclose($f);
 $conv='/usr/bin/convert../images/snotel/'. $name .'.pdf ../images/snotel/'. $name .'.jpg'; 
 system ($conv);
}
...
pdf2jpg('wsr');
Chris Kempen
9,6235 gold badges43 silver badges52 bronze badges
asked Jan 19, 2012 at 17:48
3
  • Expatiate on what happens when you call the method. Commented Jan 19, 2012 at 17:51
  • Is that your actual code? Lack of spaces after the command? Did you look into the error.log yet? Commented Jan 19, 2012 at 17:52
  • 2
    There doesn't seem to be a parameter for the $pdf variable anywhere in your function...could you elaborate? Commented Jan 19, 2012 at 17:56

1 Answer 1

1

As it is, your function tries to write the file with no data in the $pdf variable, because you did not pass it in.

You need to do one of two things:

This version takes the PDF data as an argument and creates the file in the function:

function pdf2jpg ($pdf, $name) {
 $f = fopen('../images/snotel/'.$name.'.pdf','w');
 fwrite($f,$pdf);
 fclose($f);
 $conv = '/usr/bin/convert ../images/snotel/'.$name.'.pdf ../images/snotel/'.$name.'.jpg'; 
 //run 
 system($conv);
}
// Usage
pdf2jpg($pdf, 'wsr');

This version just takes the name, assuming that the file already exists:

function pdf2jpg ($name) {
 $conv = '/usr/bin/convert ../images/snotel/'.$name.'.pdf ../images/snotel/'.$name.'.jpg'; 
 //run 
 system ($conv);
}
// Usage
$name = 'wsr';
$f = fopen('../images/snotel/'.$name.'.pdf','w');
fwrite($f,$pdf);
fclose($f);
pdf2jpg($name);
answered Jan 19, 2012 at 17:59
Sign up to request clarification or add additional context in comments.

1 Comment

Tnx all. I was (foolishly) expecting an email notification and just forgot about this question. I had figured it out on my own in the meantime.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.