【问题标题】:using DOMPDF to get PDF with jQuery使用 DOMPDF 通过 jQuery 获取 PDF
【发布时间】:2017-01-09 10:30:04
【问题描述】:

首先对不起我的英语。

我正在尝试使用 jQuery、Ajax 和 DomPDF 创建包含部分 html 的 pdf。

在我的服务器端,我使用下一个代码:

require_once './dompdf/autoload.inc.php';
if(!empty($_GET['pdf'])){
    $html=$_GET['pdf'];

    # Instanciamos un objeto de la clase DOMPDF.
    $mipdf =  new Dompdf();

    # Definimos el tamaño y orientación del papel que queremos.
    # O por defecto cogerá el que está en el fichero de configuración.
    $mipdf ->setPaper("A4", "portrait");

    # Cargamos el contenido HTML.
    $mipdf ->loadHtml(utf8_decode($html));

    # Renderizamos el documento PDF.
    $mipdf ->render();

    # Enviamos el fichero PDF al navegador.
    //$mipdf ->stream("Claustro.pdf");
    echo base64_encode($mipdf->output());
}

在客户端,在 jQuery 中我有:

$("#imprimir").click(function(){
  console.log(datos);
  $.ajax({
    type: "GET",
    dataType: 'text',
    url: "./librerias/php/funciones.php",
    data: {pdf:datos},
    success: function(pdf) {
      var modalWidth = $(window).width() - 400;
      var modalHeight = $(window).height() - 400
      var iframeWidth = modalWidth - 20;
      var iframeHeight = modalHeight - 20;
      $( "#display_dialog").html('<iframe width="' + iframeWidth + '" height="' + iframeHeight + '" src="data:application/pdf;base64,' + pdf + '"></object>');
      $( "#display_dialog" ).dialog({
        width: modalWidth,
        height: modalHeight,

        modal: true,
        close: function( event, ui ) {
          $( "#display_dialog").html("");
        }
      });
    }
  });
});//fin imprimir

我在 var "datos" 中有我想要的所有 html 代码在 pdf 中。

为了显示 iframe,我有一个隐藏的 div:

<div id="display_dialog"></div>

但我无法获取 PDF,iframe 可以工作,但没有数据。

有什么解决办法吗?有谁能够帮我?谢谢大家!

【问题讨论】:

    标签: javascript php jquery ajax dompdf


    【解决方案1】:

    为了解决这个问题,我做了一些改变:

    在服务器端:

    require 'vendor/autoload.php';
    define('UPLOAD_DIR', 'PDFs/');
    if(!empty($_POST['pdf'])){
    
        $html=$_POST['pdf'];
        @file_put_contents("texto.txt", $html);
        $name=str_replace(" ","+",$_POST['nombre']);
        $nombre = $name;
    
            # Instanciamos un objeto de la clase DOMPDF.
        $options = new Options();
        $options->setIsRemoteEnabled(true);
    
        $mipdf =  new Dompdf($options);
    
            # Definimos el tamaño y orientación del papel que queremos.
            # O por defecto cogerá el que está en el fichero de configuración.
        $mipdf ->setPaper("A4", "portrait");
    
            # Cargamos el contenido HTML.
        $mipdf ->loadHtml(utf8_decode($html));
    
            # Renderizamos el documento PDF.
        $mipdf ->render();
    
            # Enviamos el fichero PDF al navegador.
            //$mipdf ->stream("Claustro.pdf");
        $pdf=$mipdf->output();
        @file_put_contents(UPLOAD_DIR.$nombre.".pdf", $pdf);
        echo json_encode("http://regorodri.noip.me/proyecto/librerias/php/".UPLOAD_DIR.$nombre.".pdf");
        //}
    }
    

    在客户端:

    $("#imprimir").click(function(){
      var name=$("#day").val();
      $.ajax({
        type: "POST",
        dataType: 'text',
        dataType: 'json',
        url: "./librerias/php/funciones.php",
        data: {pdf:datos,nombre:name},
        success: function(pdf) {
          console.log("url->",pdf);
          window.open(pdf, '_blank');
        }
      });
    

    【讨论】:

      猜你喜欢
      • 2021-07-07
      • 1970-01-01
      • 1970-01-01
      • 2013-11-27
      • 1970-01-01
      • 2011-10-23
      • 2020-06-02
      • 2017-08-14
      • 2012-06-06
      相关资源
      最近更新 更多