dqoag62688 2015-05-05 10:21
浏览 120

使用ajax在浏览器中内联显示PDF

I know that this question is already asked many times by different ways. But still i am not able to figure out the answer. I mean i did not find a proper descriptive answer.

I use mpdf library to generate PDF. I post some data of hidden fields to a PHP script by means of ajax.

Following are the code snippets.

//ajax_file

$("#button_id").click(function(e){

var table_clone=$("#table_id").clone();
var tableData=table_clone.prop('outerHTML');//html for pdf generation
dataString='page='+tableData;

$.ajax({
         type: 'POST',
         url: 'pdfgenerator.php',
         data: dataString,
         cache: false,
        success: function(response)
        {
          //what to do here in order to display pdf
         },
         error: function(............){
           .
           .
          }
        });
});

PHP Script

//pdfgenerator.php

<?php
      include('../mpdf/mpdf.php');
      if(/*checking post items are set*/)
       {
         //retriving post items

         $mpdf=new mPDF('c','A4-L');
         $mpdf->WriteHTML($tableData);
         $mpdf->output('xyz.pdf','I');
          exit;
        }
?> 

Following are my constraints

-> I don't want to save file permanently on server (which is possible by means of 'F' option in output()).

-> I have to display it in browser from where it can be downloaded.

PHP script works correctly if called without ajax. Hence it returns correct data but i am unable to display it in pdf inside the browser.

While searching for answers i found that it is not possible by means of ajax. so is there any way around by doing something in PHP or javascript. Please provide a descriptive answer.

Thanks,

  • 写回答

1条回答 默认 最新

  • donglu2008 2015-05-07 11:20
    关注

    You can use this code

    $.ajax({
             type: 'POST',
             url: 'pdfgenerator.php',
             data: dataString,
             cache: false,
            success: function(response)
            {
              var tag ='<object width="400" height="500" type="application/pdf" data="'+xyz.pdf+'" id="show_obj1" class="obj"></object>';
              $(#pdfdiv).html(tag);
             },
             error: function(............){
               .
               .
              }
            });
    });
    

    Here #pdfdiv in $(#pdfdiv).html(tag); is the id of the div in which you want to show the pdf

    评论

报告相同问题?

悬赏问题

  • ¥20 C# TCP服务端,客户端退出后,不断有数据进来
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?