我想在wp媒体文件夹中上传PDF文件,并同时更新ACF字段

时间:2019-04-04 16:47:25

标签: php wordpress media html2pdf

我已经设置了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.

0 个答案:

没有答案