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

使用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 02: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(), "
    ";
    }
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥40 selenium访问信用中国
  • ¥15 电视大赛投票系统的c语言代码怎么做
  • ¥20 在搭建fabric网络过程中遇到“无法使用新的生命周期”的报错
  • ¥15 Python中关于代码运行报错的问题
  • ¥500 python 的API,有酬谢
  • ¥15 软件冲突问题,软件残留问题
  • ¥30 有没有人会写hLDA,有偿求写,我有一个文档,想通过hLDA得出这个文档的层次主题,有偿有偿!
  • ¥50 有没有人会写hLDA,有偿求写,我有一个文档,想通过hLDA得出这个文档的层次主题,有偿有偿!
  • ¥15 alpha101因子里哪些适合crypto?
  • ¥15 ctrl win alt 键一直触发
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部