I'm trying to create a PDF from post content, including some ACF fields. Currently I've got it working on a POST form and dynamically creating a PDF each time, but I'd like to make it generate the PDF on saving the post so I can then just have a download button which downloads the PDF generated from saving the post, but having difficulty doing so. Any help would be grand!
I've tried to create this as a function in functions.php hooking into the acf/save_post action at prioirty 15 so it's after the initial save.
function vehicle_details_pdf( $post_id ) {
require('wp-content/themes/murrayscott-nelson/fpdf/fpdf.php');
class PDF extends FPDF
{
function Header()
{
$vehicleRef = $_POST['ref'];
$this->Image('wp-content/themes/murrayscott-nelson/assets/logo.jpg',10,10,80);
$this->SetFont('Arial','B',15);
$this->Cell(80);
$this->Cell(85,10,'Vehicle Details - Ref:' . $vehicleRef ,0,0,'R');
$this->Ln(20);
}
function Footer()
{
$this->SetY(-15);
$this->SetFont('Arial','I',8);
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
$vehicleName = get_the_title();
$vehicleBodyStyle = get_field('body_type', $post_id);
$vehiclePrice = get_field('price', $post_id);
$vehicleEngCap = get_field('engine_capacity', $post_id);
$vehicleMileage = get_field('mileage', $post_id);
$vehicleSteering = get_field('steering', $post_id);
$vehicleDescription = get_field('description', $post_id);
$vehicleRef = get_field('reg_ref', $post_id);
$images = get_field('vehicle_images', $post_id);
$image = $images[0];
$image = $image['url'];
$vehicleImage = preg_replace("/^https:/i", "http:", $image);
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial','B',17);
$pdf->Image( $vehicleImage, $pdf->GetX(), $pdf->GetY(), 190);
$pdf->Ln(130);
$pdf->Cell(0,10,$vehicleName);
$pdf->Ln(10);
$pdf->SetFont('Arial','',12);
$pdf->Cell(0,7,'Body Style: ' . $vehicleBodyStyle . ' | Steering: ' . $vehicleSteering . ' | Engine Capacity: ' . $vehicleEngineCap . 'cc | Mileage: ' . $vehicleMileage . ' miles' );
$pdf->Ln(7);
$pdf->Write (6, $vehicleDescription);
$pdf->SetFont('Arial','B',17);
$pdf->Ln(0);
$pdf->Cell( 0, 10, iconv("UTF-8", "ISO-8859-1", "£") . $vehiclePrice, 0);
$pdf->Ln(10);
$pdf->setFillColor(230,230,230);
$pdf->SetFont('Arial','B',15);
$pdf->MultiCell(0,7,"If you'd like to arrange a viewing please call 01723 361 227",0,'C',1);
$pdf->MultiCell(0,7,"quoting reference: " . $vehicleRef ,0,'C',1);
$pdf->setFillColor(38,54,29);
$pdf->setTextColor(255,255,255);
$pdf->Ln(5);
$pdf->MultiCell(0,7,"Finance Available - Ask for details" ,0,'C',1);
$pdf->setTextColor(0,0,0);
$filepath= wp_get_upload_dir() . '/vehicledetails/' ;
$pdf->Output('F', $filepath . $vehicleRef . '.pdf');
}
add_action('acf/save_post', 'vehicle_details_pdf', 15);
I expect the code to create a pdf file in the wp-content/uploads/vehicledetails directory
error messages:
[01-Sep-2019 10:49:10 UTC] PHP Warning: Invalid argument supplied for
foreach() in /app/public/wp-includes/class-wp-post-type.php on line
597 [01-Sep-2019 10:49:10 UTC] PHP Warning: Invalid argument supplied
for foreach() in /app/public/wp-includes/class-wp-post-type.php on
line 597 [01-Sep-2019 10:49:15 UTC] PHP Warning: Invalid argument
supplied for foreach() in
/app/public/wp-includes/class-wp-post-type.php on line 597
[01-Sep-2019 10:49:15 UTC] PHP Warning: Invalid argument supplied for
foreach() in /app/public/wp-includes/class-wp-post-type.php on line
597 [01-Sep-2019 10:49:17 UTC] PHP Warning: Invalid argument supplied
for foreach() in /app/public/wp-includes/class-wp-post-type.php on
line 597 [01-Sep-2019 10:49:17 UTC] PHP Warning: Invalid argument
supplied for foreach() in
/app/public/wp-includes/class-wp-post-type.php on line 597
[01-Sep-2019 10:49:17 UTC] PHP Warning: Invalid argument supplied for
foreach() in /app/public/wp-includes/class-wp-post-type.php on line
597 [01-Sep-2019 10:49:18 UTC] PHP Warning: Invalid argument supplied
for foreach() in /app/public/wp-includes/class-wp-post-type.php on
line 597 [01-Sep-2019 10:49:19 UTC] PHP Warning: Invalid argument
supplied for foreach() in
/app/public/wp-includes/class-wp-post-type.php on line 597
[01-Sep-2019 10:49:19 UTC] PHP Warning: Invalid argument supplied for
foreach() in /app/public/wp-includes/class-wp-post-type.php on line
597 [01-Sep-2019 10:49:20 UTC] PHP Warning: Invalid argument supplied
for foreach() in /app/public/wp-includes/class-wp-post-type.php on
line 597 [01-Sep-2019 10:49:20 UTC] PHP Warning: Invalid argument
supplied for foreach() in
/app/public/wp-includes/class-wp-post-type.php on line 597
[01-Sep-2019 10:49:21 UTC] PHP Warning:
require(wp-content/themes/murrayscott-nelson/fpdf/fpdf.php): failed to
open stream: No such file or directory in
/app/public/wp-content/themes/murrayscott-nelson/functions.php on line
81 [01-Sep-2019 10:49:21 UTC] PHP Warning:
require(wp-content/themes/murrayscott-nelson/fpdf/fpdf.php): failed to
open stream: No such file or directory in
/app/public/wp-content/themes/murrayscott-nelson/functions.php on line
81 [01-Sep-2019 10:49:21 UTC] PHP Fatal error: require(): Failed
opening required 'wp-content/themes/murrayscott-nelson/fpdf/fpdf.php'
(include_path='.:/usr/share/php:/www/wp-content/pear') in
/app/public/wp-content/themes/murrayscott-nelson/functions.php on line
81 [01-Sep-2019 10:49:21 UTC] PHP Fatal error: require(): Failed
opening required 'wp-content/themes/murrayscott-nelson/fpdf/fpdf.php'
(include_path='.:/usr/share/php:/www/wp-content/pear') in
/app/public/wp-content/themes/murrayscott-nelson/functions.php on line
81 [01-Sep-2019 10:50:16 UTC] PHP Warning: Invalid argument supplied
for foreach() in /app/public/wp-includes/class-wp-post-type.php on
line 597 [01-Sep-2019 10:52:17 UTC] PHP Warning: Invalid argument
supplied for foreach() in
/app/public/wp-includes/class-wp-post-type.php on line 597
[01-Sep-2019 10:54:18 UTC] PHP Warning: Invalid argument supplied for
foreach() in /app/public/wp-includes/class-wp-post-type.php on line
597 [01-Sep-2019 10:56:18 UTC] PHP Parse error: syntax error,
unexpected '$vehicleName' (T_VARIABLE), expecting function
(T_FUNCTION) or const (T_CONST) in
/app/public/wp-content/themes/murrayscott-nelson/functions.php on line
114 [01-Sep-2019 10:58:19 UTC] PHP Parse error: syntax error,
unexpected '$vehicleName' (T_VARIABLE), expecting function
(T_FUNCTION) or const (T_CONST) in
/app/public/wp-content/themes/murrayscott-nelson/functions.php on line
114
Your upload directory is not set correctly.
Try this:
$uploads = wp_upload_dir();
$pdf->Output('F', $uploads['basedir'] . '/vehicledetails/' . $vehicleRef . '.pdf');
I have registration.php in my data folder which is in the source folder so that isn't the issue. What I'm doing is recording my website's form data $dataRecord and trying to save it in this csv file that I have.
But I'm getting four errors:
Warning: fopen(data/registration.csv): failed to open stream: No such
file or directory in /users/s/j/sjflanag/www-root/cs008/lab11/form.php
on line 225
Warning: fputcsv() expects parameter 1 to be resource, boolean given
in /users/s/j/sjflanag/www-root/cs008/lab11/form.php on line 228
Warning: fclose() expects parameter 1 to be resource, boolean given in
/users/s/j/sjflanag/www-root/cs008/lab11/form.php on line 231
Fatal error: Call to undefined function sendMail() in
/users/s/j/sjflanag/www-root/cs008/lab11/form.php on line 273
$myFolder = 'data/';
$myFileName = 'registration';
$fileExt = '.csv';
$filename = $myFolder . $myFileName . $fileExt;
if ($debug) {
print PHP_EOL . '<p>filename is ' . $filename;
}
$file = fopen($filename, 'a');
fputcsv($file, $dataRecord);
fclose($file);
Warning: file_get_contents(http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast) [function.file-get-contents]: failed to open stream: HTTP request failed! in /home/content/93/11389393/html/white/bet_gg.php on line 286
$grabPrice = file_get_contents('http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast');
$data = json_decode($grabPrice);
$current_price = $data->data->last_local->value;
Check if allow_url_fopen is enabled in PHP
If not - use cURL to grab some page by URL - works great
check if allow_url_fopen is enabled
phpinfo();
or
if( ini_get('allow_url_fopen') ) {
echo "Enable";
} else{
echo "Not enable";
}
if no, in your php.ini change
allow_url_fopen =0;
to
allow_url_fopen =1;
If i push a string which doesn't exist or misspelled to function benzer($ilk_artist) i am getting errors bottom. Whether i push a valid artist name or not variable $completeurl is always defined. So i can't put if($completeurl) I think i should control whether $completeurl is valid before simplexml_load_file($completeurl). Do you have an idea that how i can do it?
function benzer($ilk_artist)
{
$completeurl = 'http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist='.trim($ilk_artist).'&api_key='.LASTFM_APP_ID;
$completeurl = urlencode($completeurl);
$xml = simplexml_load_file($completeurl);
if(!$xml)
{
return false;
}
$artists = $xml->similarartists->artist;
$length = count($artists);
for ($i = 0; $i < $length; $i++) {
$artistname[$i] = $artists[$i]->name;
}
return simplexml_kurtul($artistname);
}
errors:
[17-Dec-2012 11:43:33] PHP Warning: simplexml_load_file(http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist=tgyu+6uh6n&api_key=APIKEY) [<a href='function.simplexml-load-file'>function.simplexml-load-file</a>]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request
in /home6/.../public_html/.../functions/fns.php on line 102
[17-Dec-2012 11:43:33] PHP Warning: simplexml_load_file() [<a href='function.simplexml-load-file'>function.simplexml-load-file</a>]: I/O warning : failed to load external entity "http%3A%2F%2Fws.audioscrobbler.com%2F2.0%2F%3Fmethod%3Dartist.getsimilar%26artist%3Dtgyu+6uh6n%26api_key=APIKEY0" in /home6/.../public_html/.../functions/fns.php on line 102
What about not printing out the warnings by adding '#'?
$xml = #simplexml_load_file($completeurl);
if(!$xml)
{
return false;
}
i load file from server:
$url = 'http://www.sample.com/test.xml';
$xml = simplexml_load_file($url);
And if servers is close i get error:
Warning: simplexml_load_file() [function.simplexml-load-file]: php_network_getaddresses: getaddrinfo failed:...
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity
how to check if the file is reached?
From the manual page for simplexml_load_file
Returns an object of class SimpleXMLElement with properties containing the data held within the XML document. On errors, it will return FALSE.
which means you can do
$url = 'http://www.sample.com/test.xml';
$xml = simplexml_load_file($url);
// check what was returned
if(FALSE === $xml) {
echo 'could not open file';
}