2017-12-29 28 views
0

Ich habe ein Formular, das mit meinem benutzerdefinierten Plugin angezeigt wird. unten angegeben ist Codeform angezeigt Plugin:Wie erstelle ich PDF von einer HTML-Seite

$question = $wpdb->get_results("SELECT * FROM $table_name1 WHERE group_id=$id"); 
$instruction = $question[0]->instructions; 
$html = "<h2>Instructions</h2><p>".$instruction."</p><br><button>Download</button>"; 
$arr_size = sizeof($question); 
$html .= "<form method='POST' action=''>"; 
for ($i=0; $i < $arr_size ; $i++) { 
    $sid = $question[$i]->s_id; 
    $html .= "<b>Question:</b><i><span>".$question[$i]->questions."</span></i><br>"; 
    $answer = $wpdb->get_results("SELECT * FROM $table_name2 WHERE u_id=$userid AND s_id=$sid"); 
    $size = sizeof($answer); 
    $option = $question[$i]->options; 
    for ($j=1,$k=0; $j <= $option; $j++,$k++) { 
     $html .= "<input type='hidden' value='".$answer[$k]->a_id."' name='aid".$sid.$j."'>"; 
     $html .= "<textarea name='ans".$sid.$j."' id='ans".$sid.$j."'>".$answer[$k]->answer."</textarea><br>"; 
    } 
} 
$html .= "<input type='submit' value='submit' name='submit2'></form>"; 
return $html; 

Zurück vorgelegten Werte ihre in die Textfelder sein. Ich muss den Inhalt des Formulars herunterladen, wenn ich auf den Download-Button klicke, nicht auf die Formularübergabe.

+0

Welche Sie pdf "dompdf" verwendet? –

+0

Hoffe das wird dir helfen. http://www.c-sharpcorner.com/blogs/how-to-convert-html-with-css-to-pdf-using-javascript –

+0

@Sandra, überprüfen Sie diese URL Ich hoffe, es ist hilfreich: - http: //w3init.com/php/libraries/generate-pdf-from-html-code-in-php-using-mpdf/ UND https://www.phpflow.com/php/create-pdf-file -using-mpdf-php/ –

Antwort

0

Bitte überprüfen Sie, dass Code

https://www.phpgang.com/how-to-convert-html-to-pdf-in-php-with-fpdf_447.html?doing_wp_cron=1514545193.9442338943481445312500

Sie haben fpdf Bibliothek herunterladen und es in Ihrer PHP-Datei unter Einstellungen und zeigen, wie Tags, Schriften und Bilder in Ihrer PDF-Datei. Mit der fpdf-Bibliothek haben wir eine HTMLparser-Bibliothek verwendet, die von Programmierern und allen anderen Bibliotheken, die hier verfügbar sind, zur Verfügung gestellt wird, die Sie herunterladen und entsprechend Ihren Anforderungen verwenden können.

index.html:

<html> 
<head> 
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen"> 
<link href="css/bootstrap-responsive.min.css" rel="stylesheet" media="screen"> 
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script> 
<script type="text/javascript" src="js/bootstrap.min.js"></script> 
<title>How to create Contact Form using Bootstrap | PGPGang.com</title> 
</head> 

<body> 
<h2>How to create Contact Form using Bootstrap example.&nbsp;&nbsp;&nbsp;=> <a href="https://www.phpgang.com/">Home</a> | <a href="http://demo.phpgang.com/">More Demos</a></h2> 
<div class="container"> 
     <form class="contact-us form-horizontal" action="actionpdf.php" method="post"> 
     <legend>Fill Form and submit to generate PDF</legend>   
     <div class="control-group"> 
      <label class="control-label">Name</label> 
      <div class="controls"> 
       <div class="input-prepend"> 
       <span class="add-on"><i class="icon-user"></i></span> 
        <input type="text" class="input-xlarge" name="name" placeholder="Name"> 
       </div> 
      </div> 
     </div> 
     <div class="control-group"> 
      <label class="control-label">Email</label> 
      <div class="controls"> 
       <div class="input-prepend"> 
       <span class="add-on"><i class="icon-envelope"></i></span> 
        <input type="text" class="input-xlarge" name="email" placeholder="Email"> 
       </div> 
      </div>  
     </div> 
     <div class="control-group"> 
      <label class="control-label">Url</label> 
      <div class="controls"> 
       <div class="input-prepend"> 
       <span class="add-on"><i class="icon-globe"></i></span> 
        <input type="text" id="url" class="input-xlarge" name="url" placeholder="http://www.example.com"> 
       </div> 
      </div> 
     </div> 
     <div class="control-group"> 
      <label class="control-label">Comment</label> 
      <div class="controls"> 
       <div class="input-prepend"> 
       <span class="add-on"><i class="icon-pencil"></i></span> 
        <textarea name="comment" class="span4" rows="4" cols="80" placeholder="Comment (Max 200 characters)"></textarea> 
       </div> 
      </div> 
     </div> 
     <div class="control-group"> 
      <div class="controls"> 
      <button type="submit" class="btn btn-primary">Submit</button> 
      <button type="button" class="btn">Cancel</button> 
      </div>  
     </div> 
     </form> 
</div> 
</body> 
</html> 

actionpdf.php

require('WriteHTML.php'); 

$pdf=new PDF_HTML(); 

$pdf->AliasNbPages(); 
$pdf->SetAutoPageBreak(true, 15); 

$pdf->AddPage(); 
$pdf->Image('logo.png',18,13,33); 
$pdf->SetFont('Arial','B',14); 
$pdf->WriteHTML('<para><h1>PHPGang Programming Blog, Tutorials, jQuery, Ajax, PHP, MySQL and Demos</h1><br> 
Website: <u>www.phpgang.com</u></para><br><br>How to Convert HTML to PDF with fpdf example'); 

$pdf->SetFont('Arial','B',7); 
$htmlTable='<TABLE> 
<TR> 
<TD>Name:</TD> 
<TD>'.$_POST['name'].'</TD> 
</TR> 
<TR> 
<TD>Email:</TD> 
<TD>'.$_POST['email'].'</TD> 
</TR> 
<TR> 
<TD>URl:</TD> 
<TD>'.$_POST['url'].'</TD> 
</TR> 
<TR> 
<TD>Comment:</TD> 
<TD>'.$_POST['comment'].'</TD> 
</TR> 
</TABLE>'; 
$pdf->WriteHTML2("<br><br><br>$htmlTable"); 
$pdf->SetFont('Arial','B',6); 
$pdf->Output(); 
?> 
+0

Ich muss PDF auf Knopfdruck nicht auf Formular-Übermittlung generieren. – Sandra

+0

Bitte überprüfen Sie den Link, der nicht Formular Einreichung ist seine einfache Schaltfläche Sie können versuchen, dass auf jeder Schaltfläche ausführen –

+0

Das Formular in diesem Link haben Aktion zu 'actionpdf.php'. Die Formularübermittlung in meinem Plugin benutzt Ajax, also gibt es keine Aktion für meine von. – Sandra

0

Ich würde fpdf für diese super einfache Aufgabe nicht empfehlen. Versuchen https://wkhtmltopdf.org/

in Ihrem Code wird es einfach sein:

$htmlFileName = '/tmp/file.html'; 
file_put_contents($html, $content); 
exec("wkhtmltopdf portrait $htmlFileName $pdfFileName"); 
Verwandte Themen