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 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝