首页 > 解决方案 > 如何在后期保存或更新时生成 pdf

问题描述

我正在尝试从帖子内容创建 PDF,包括一些 ACF 字段。目前我已经让它在一个 POST 表单上工作并每次动态创建一个 PDF,但我想让它在保存帖子时生成 PDF,这样我就可以有一个下载按钮来下载保存生成的 PDF帖子,但这样做有困难。任何帮助都会很重要!

我试图在functions.php中将它创建为一个函数,并在prioirty 15连接到acf/save_post操作,所以它是在初始保存之后。


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);

我希望代码在 wp-content/uploads/vehicledetails 目录中创建一个 pdf 文件

错误信息:

[01-Sep-2019 10:49:10 UTC] PHP 警告:在 /app/public/wp-includes/class-wp-post-type.php 第 597 行 [01-Sep- 2019 年 10:49:10 UTC] PHP 警告:在第 597 行 [01-Sep-2019 10:49: 15 UTC] PHP 警告:在第 597 行的 /app/public/wp-includes/class-wp-post-type.php 中为 foreach() 提供的参数无效 [01-Sep-2019 10:49:15 UTC] PHP 警告:在第 597 行 [01-Sep-2019 10:49:17 UTC] 的 /app/public/wp-includes/class-wp-post-type.php 中为 foreach() 提供了无效参数 PHP 警告:为/app/public/wp-includes/class-wp-post-type.php 中的 foreach() 第 597 行 [01-Sep-2019 10:49:17 UTC] PHP 警告:在 /app/public/wp-includes/class-wp-post-type.php 第 597 行 [01-Sep-2019 10:49:17 UTC] PHP 警告:为 foreach 提供的参数无效() 在 /app/public/wp-includes/class-wp-post-type.php 第 597 行 [01-Sep-2019 10:49:18 UTC] PHP 警告:为 /app 中的 foreach() 提供的参数无效/public/wp-includes/class-wp-post-type.php 在第 597 行 [01-Sep-2019 10:49:19 UTC] PHP 警告:在 /app/public/wp- 中为 foreach() 提供的参数无效includes/class-wp-post-type.php 在第 597 行 [01-Sep-2019 10:49:19 UTC] PHP 警告:在 /app/public/wp-includes/class-wp 中为 foreach() 提供的参数无效-post-type.php 第 597 行 [01-Sep-2019 10:49:20 UTC] PHP 警告:在 /app/public/wp-includes/class-wp-post-type 中为 foreach() 提供的参数无效。php on line 597 [01-Sep-2019 10:49:20 UTC] PHP Warning: Invalid argument provided for foreach() in /app/public/wp-includes/class-wp-post-type.php on line 597 [ 2019 年 9 月 1 日 10:49:21 UTC] PHP 警告:需要(wp-content/themes/murrayscott-nelson/fpdf/fpdf.php):无法打开流:/app/public/ 中没有这样的文件或目录wp-content/themes/murrayscott-nelson/functions.php 在第 81 行 [01-Sep-2019 10:49:21 UTC] PHP 警告:需要(wp-content/themes/murrayscott-nelson/fpdf/fpdf.php) :无法打开流:第 81 行的 /app/public/wp-content/themes/murrayscott-nelson/functions.php [01-Sep-2019 10:49:21 UTC] 中没有这样的文件或目录 PHP 致命错误:要求():打开失败需要'wp-content/themes/murrayscott-nelson/fpdf/fpdf.php'(include_path='.:/usr/share/php:/www/wp-content/pear') 在 /app/public/wp-content/themes/murrayscott-nelson/functions.php 第 81 行 [01-Sep-2019 10:49:21 UTC] PHP 致命错误:require():打开失败需要 'wp- /app/public/wp-content/themes/murrayscott 中的内容/主题/murrayscott-nelson/fpdf/fpdf.php' (include_path='.:/usr/share/php:/www/wp-content/pear') -nelson/functions.php 在第 81 行 [01-Sep-2019 10:50:16 UTC] PHP 警告:在 /app/public/wp-includes/class-wp-post-type 中为 foreach() 提供的参数无效。 php 在第 597 行 [01-Sep-2019 10:52:17 UTC] PHP 警告:在第 597 行的 /app/public/wp-includes/class-wp-post-type.php 中为 foreach() 提供的参数无效 [ 2019 年 9 月 1 日 10:54:18 UTC] PHP 警告:在第 597 行的 /app/public/wp-includes/class-wp-post-type.php 中为 foreach() 提供的参数无效 [2019 年 9 月 1 日10:56:18 UTC] PHP 解析错误:语法错误,意外的'$vehicleName' (T_VARIABLE),期待函数 (T_FUNCTION) 或 const (T_CONST) 在 /app/public/wp-content/themes/murrayscott-nelson/functions.php 第 114 行 [01-Sep-2019 10:58:19 UTC] PHP 解析错误:语法错误,意外的 '$vehicleName' (T_VARIABLE),在 /app/public/wp-content/themes/murrayscott-nelson/functions 中期待函数 (T_FUNCTION) 或 const (T_CONST) .php 在第 114 行在第 114 行的 /app/public/wp-content/themes/murrayscott-nelson/functions.php 中期待函数 (T_FUNCTION) 或 const (T_CONST)在第 114 行的 /app/public/wp-content/themes/murrayscott-nelson/functions.php 中期待函数 (T_FUNCTION) 或 const (T_CONST)

标签: phpwordpresspdfadvanced-custom-fieldsfpdf

解决方案


您的上传目录设置不正确。

试试这个:

$uploads = wp_upload_dir();
$pdf->Output('F', $uploads['basedir'] . '/vehicledetails/' . $vehicleRef . '.pdf');

推荐阅读