首页 > 解决方案 > 我想在 wp 媒体文件夹中上传 PDF 文件并更新 ACF 字段

问题描述

我已经设置了 HTML 2 PDF 代码,并且我已经通过 html2pdfrocket API 添加了这个,当我运行这个 API 时,PDF 被下载,这可以,我希望 PDF 下载和上传也在 wordpress 媒体库中。

  // Set parameters

  $postdata = http_build_query(
       array(
          'apikey' => $apikey,
          'value' => $value,
          'MarginBottom' => '10',
      'MarginLeft' => '10',
      'MarginRight' => '10',
          'MarginTop' => '10'
    )
  );

  $opts = array('http' =>
     array(
         'method'  => 'POST',
         'header'  => 'Content-type: application/x-www-form-urlencoded',
         'content' => $postdata
     )
  );

  $context  = stream_context_create($opts);

  // Convert the HTML string to a PDF using those parameters
  $result = file_get_contents('http://api.html2pdfrocket.com/pdf', false, 
  $context);


  header('Content-Description: File Transfer');
  header('Content-Type: application/pdf');
  header('Expires: 0');
  header('Cache-Control: must-revalidate');
  header('Pragma: public');
  header('Content-Length: ' . strlen($result));

  header('Content-Disposition: attachment; filename=' . 'My CV.pdf' );

  $results = file_get_contents("http://api.html2pdfrocket.com/pdf?apikey=" . urlencode($apikey) . "&value=" . urlencode($value));

  update_field('pdf_file', 'sdsd', 'user_'.$current_user->ID);

  echo $result;   //echo result auto download PDF but i want upload also.

标签: phpwordpressmediahtml2pdf

解决方案


推荐阅读