dsj60862 2017-06-02 10:08
浏览 280
已采纳

使用wkhtmltopdf在PDF转换后更改的值未显示

Currently inside my html page I change some values, the way it works is like this:

The function:

function change1(){
   var myNewTitle = document.getElementById('myTextField1').value;
   if( myNewTitle.length==0 ){
       alert('Write Some real Text please.');
   return;
   }
   var titles = document.getElementsByClassName('title1');
   Array.prototype.forEach.call(titles,title => {
    title.innerHTML = myNewTitle;
   });
}

The values that get changed and the textfield used for input:

      Voornaam: <h3 class="title1">Kevin</h3>
      <input type="text" id="myTextField1"/>
      <input type="submit" id="byBtn" value="Change" onclick="change1()"/><br/>

      <p class="title1">Test.</p>

And I convert using wkhtmltopdf, which is a command line tool for html to PDF conversion. However the changes I make are ofcourse not done on the server but locally, and this is why my changes do not show in my conversion.

I was thinking about downloading the changed html page first and then convert it with wkhtmltopdf. Would anybody be able to give me an example of how this could be done or perhaps some me another and better way?

  • 写回答

1条回答 默认 最新

  • doushao1087 2017-06-02 10:45
    关注

    You have to send over the page content and the wkhtmltopdfon the server will do the job.

    On the client side you need to all a function like this:

    function send(){ 
      var f = document.createElement("form"),
         i = document.createElement("input"); //input element, hidden
    
      f.setAttribute('method',"post");
      f.setAttribute('action',"http://yourserver.com/script.php");
    
      i.setAttribute('type',"hidden");
      i.setAttribute('name',"content");
      i.setAttribute('value', document.body.innerHTML); // or the container you need
    
      f.appendChild(i);
      document.getElementsByTagName('body')[0].appendChild(f);
      f.submit();
    };
    

    On the server side, in php for example, you will get the content, save it on temp file and then call the tool to convert it to pdf and then return the pdf to the client:

    <?php
    try {
       $content = $_REQUEST['content'];
        if(!file_exists('/tmp') ){
           mkdir('/tmp', 0777);
        }
       $fp = fopen('w+','/tmp/tmp.html');
       if($fp){
         fwrite($fp, $content);
         fclose($fp);
    
         $filename = '/tmp/out_' . time() .'.pdf'; // output filename
         shell_exec('wkhtmltopdf /tmp/tmp.html ' . $filename);
    
         //then eventually ask user for download the result
         header("Content-type:application/pdf");
    
         // It will be called output.pdf
         header("Content-Disposition:attachment;filename='output.pdf'");
    
        readfile($filename);
      }else{
         echo 'html file could not be created';
      }
    } catch (Exception $e) {
      echo 'exception: ',  $e->getMessage(), "
    ";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答