如何在DOMPDF中使用php变量生成动态PDF?

发布于 2025-01-15 12:41:05 字数 4993 浏览 3 评论 0原文

下面是我使用 DOMPDF 将 HTML 生成为 PDF 的代码,它将生成一个静态 pdf 到这里完美工作,但是当我添加了一些 PHP 变量(在代码中我使用会话变量)来获取用户的动态数据这不会渲染 PDF。 在将 HTML 文件转换为最终 PDF 之前,是否有其他方法可以使用 DOMPDF 将一些动态数据添加到 HTML 文件中?

<?php
//$first_name = "Gopal Kshirsagar" 
session_start();
$_SESSION["mandatory_date"]= $mandatory_date;
$_SESSION["first_name"]= $first_name;
$_SESSION["last_name"]= $last_name;
?>

<?php
use Dompdf\Options;
//add autoload.inc.php
require_once "./dompdf/autoload.inc.php";

// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();

//$dompdf->loadhtml($aData['html']);
$dompdf->set_option('isRemoteEnabled', TRUE);
//$dompdf->loadHtml($html);

$dompdf->loadHtml('
<style> 
   div.background {
      background: #ffffff;
      border: 4px solid black;
      margin: 10px;
    }
    
    div.transbox {
      margin: 3px;
      background-color: #ffffff;
      border: 1px solid black;
    }
    
    div.transbox p {
      margin: 1%;
      color: #000000;
    }
    #middle {
      margin: 5px;
      background-color: #ffffff;
      border: 1px solid #fff;
    }
    body{
       background-color: #f6f6f6;
    }
    .logowhite{
     float: right;
    }
    .logo {
       clear: right;
       text-align: center;
   }
      </style>
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css"/>
   <link href="https://vip-organon-us-hcp-com-develop.go-vip.net/nexplanontraining/wp-content/themes/twentytwentyone-child/assets/css/main.min2.css" media="all" type="text/css" rel="stylesheet"/>         
   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
   <body id="thank-you" class="base"><div id="middle">
   <div class="background">
      <div class="transbox">
         <!----Below Images I have embedded and convert into base64 (using online converter https://base64.guru/converter/encode/image)-->
         <div  class="logowhite"> <img alt="" src="org_logo.jpg" /></div> 
         <div class="logo"> <img src="nexplanon.jpg" alt="" />  
           <p style="font-size:1.35rem;color:#00F;text-transform: uppercase;"><?php echo $first_name; ?> &nbsp; <?php echo $last_name; ?></p> 
           <p style="font-size: 1.25rem;line-height:25px;">This certificate serves as confirmation of the completion of the Mandatory Training Module<BR>
             on the updated insertion and removal procedures for NEXPLANON.</p> 
            
             <div style="width: 70%;margin: auto;"> 
               <p style="float: left;font-size: 1.25rem;"><B>Training Date:</B> <span style="color: #00F;"><?php echo $mandatory_date; ?></span> </p>
               <img style="float:right;" alt="" src="sign.jpg" />   </div>
           
           </div>
           <div style="width: 90%;margin: auto;"> 
            <p style="clear: both;margin-bottom: 0%;line-height:18px;"><BR>I attested that I completed the Organon-sponsored Mandatory Training Module on the updated insertion and removal procedures for NEXPLANON in its entirety. I understand that only healthcare providers who have completed the
            Organon-sponsored Clinical Training Program for NEXPLANON may order, insert, and remove NEXPLANON.<BR><BR>
            I certified that I am authorized to perform the procedures entailed in the insertion and removal of NEXPLANON in the
            jurisdiction where I practice. I attest that if there are specific state requirements in the state where I practice, I have met all appropriate state conditions including but not limited to collaborative or signing agreement with an MD/DO.<BR><BR>
            If I am a resident or student in advanced practice training, I understand that I should only administer NEXPLANON under the supervision of an attending healthcare provider who has also been trained on the procedures to insert and
            remove NEXPLANON.</p>
         </div><br/>
     
      <div style="padding-bottom: -2%;">
      <p style="font-size: 0.7rem;float: left;">© 2021 Organon group of companies. All rights reserved. ORGANON and the ORGANON Logo are trademarks of the Organon group of companies.</p>
      <p style="float: right;font-size: 0.7rem;">US-XPL-115639 12/21</p>
     <p style="clear: both;margin-bottom: 0%;"> </p>
     </div>
      
     
     </div>
   </div>
   </div></body>
');


// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
$dompdf->stream("training-certificate", array("Attachments"=>0));
?>

Below is my code to generate HTML into PDF using DOMPDF, It will be generating a static pdf up to here perfectly working fine, but when I have added some PHP variables (in the code I am using session variables) to get users' dynamic data that will not render PDF.
Is there any other way to add some dynamic data into the HTML file using DOMPDF before it converts into a final PDF?

<?php
//$first_name = "Gopal Kshirsagar" 
session_start();
$_SESSION["mandatory_date"]= $mandatory_date;
$_SESSION["first_name"]= $first_name;
$_SESSION["last_name"]= $last_name;
?>

<?php
use Dompdf\Options;
//add autoload.inc.php
require_once "./dompdf/autoload.inc.php";

// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();

//$dompdf->loadhtml($aData['html']);
$dompdf->set_option('isRemoteEnabled', TRUE);
//$dompdf->loadHtml($html);

$dompdf->loadHtml('
<style> 
   div.background {
      background: #ffffff;
      border: 4px solid black;
      margin: 10px;
    }
    
    div.transbox {
      margin: 3px;
      background-color: #ffffff;
      border: 1px solid black;
    }
    
    div.transbox p {
      margin: 1%;
      color: #000000;
    }
    #middle {
      margin: 5px;
      background-color: #ffffff;
      border: 1px solid #fff;
    }
    body{
       background-color: #f6f6f6;
    }
    .logowhite{
     float: right;
    }
    .logo {
       clear: right;
       text-align: center;
   }
      </style>
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css"/>
   <link href="https://vip-organon-us-hcp-com-develop.go-vip.net/nexplanontraining/wp-content/themes/twentytwentyone-child/assets/css/main.min2.css" media="all" type="text/css" rel="stylesheet"/>         
   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
   <body id="thank-you" class="base"><div id="middle">
   <div class="background">
      <div class="transbox">
         <!----Below Images I have embedded and convert into base64 (using online converter https://base64.guru/converter/encode/image)-->
         <div  class="logowhite"> <img alt="" src="org_logo.jpg" /></div> 
         <div class="logo"> <img src="nexplanon.jpg" alt="" />  
           <p style="font-size:1.35rem;color:#00F;text-transform: uppercase;"><?php echo $first_name; ?>   <?php echo $last_name; ?></p> 
           <p style="font-size: 1.25rem;line-height:25px;">This certificate serves as confirmation of the completion of the Mandatory Training Module<BR>
             on the updated insertion and removal procedures for NEXPLANON.</p> 
            
             <div style="width: 70%;margin: auto;"> 
               <p style="float: left;font-size: 1.25rem;"><B>Training Date:</B> <span style="color: #00F;"><?php echo $mandatory_date; ?></span> </p>
               <img style="float:right;" alt="" src="sign.jpg" />   </div>
           
           </div>
           <div style="width: 90%;margin: auto;"> 
            <p style="clear: both;margin-bottom: 0%;line-height:18px;"><BR>I attested that I completed the Organon-sponsored Mandatory Training Module on the updated insertion and removal procedures for NEXPLANON in its entirety. I understand that only healthcare providers who have completed the
            Organon-sponsored Clinical Training Program for NEXPLANON may order, insert, and remove NEXPLANON.<BR><BR>
            I certified that I am authorized to perform the procedures entailed in the insertion and removal of NEXPLANON in the
            jurisdiction where I practice. I attest that if there are specific state requirements in the state where I practice, I have met all appropriate state conditions including but not limited to collaborative or signing agreement with an MD/DO.<BR><BR>
            If I am a resident or student in advanced practice training, I understand that I should only administer NEXPLANON under the supervision of an attending healthcare provider who has also been trained on the procedures to insert and
            remove NEXPLANON.</p>
         </div><br/>
     
      <div style="padding-bottom: -2%;">
      <p style="font-size: 0.7rem;float: left;">© 2021 Organon group of companies. All rights reserved. ORGANON and the ORGANON Logo are trademarks of the Organon group of companies.</p>
      <p style="float: right;font-size: 0.7rem;">US-XPL-115639 12/21</p>
     <p style="clear: both;margin-bottom: 0%;"> </p>
     </div>
      
     
     </div>
   </div>
   </div></body>
');


// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
$dompdf->stream("training-certificate", array("Attachments"=>0));
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文