首页 > 解决方案 > HTML/PHP 到 PDF?如何....?

问题描述

我有一些带有表格和 1 个 img 的页面,我想创建一个按钮以使其成为 pdf。问题是,它是希伯来语(utf-8 编码)。有什么办法吗?我尝试使用 jsPDF,但它不支持希伯来语。

基本上我想做的是一个按钮,它将创建一个我将通过电子邮件作为附件发送的 pdf 文件。

有什么建议么?

谢谢!

这是我尝试过的:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>
<script>
    function demoFromHTML() {
        var pdf = new jsPDF('p', 'pt', 'letter');
        source = $('#content')[0];

        specialElementHandlers = {
            // element with id of "bypass" - jQuery style selector
            '#bypassme': function (element, renderer) {
                // true = "handled elsewhere, bypass text extraction"
                return true
            }
        };
        margins = {
            top: 80,
            bottom: 60,
            left: 40,
            width: 522
        };
        pdf.fromHTML(
            source, // HTML string or DOM elem ref.
            margins.left, // x coord
            margins.top, { // y coord
                'width': margins.width, // max width of content on PDF
                'elementHandlers': specialElementHandlers
            },
            function (dispose) {
                pdf.save('Test.pdf');
            }, margins
        );
    }
</script>

<a href="javascript:demoFromHTML()" class="button">Run Code</a>
<div id="content">
HTML HERE
</div>

标签: htmlpdf

解决方案


我建议你使用这个 fpdf 工具,它使用起来非常简单。这是我的代码的演示

if(isset($_POST['pdf'])){
    require('fpdf/fpdf.php');
    $sname = $_POST['sname'];
    $emailid = $_POST['emailid'];
    $joindate = $_POST['joindate'];
    $contact = $_POST['contact'];
    $birthdate = $_POST['birthdate'];
    $about = $_POST['about'];
    $today = date("Y");


    $pdf = new FPDF();
    $pdf -> AddPage();
    $pdf->Image('logo.png',85,6,40);
    // Line break
    $pdf->Ln(20);
    // Arial bold 15
    $pdf->SetFont('Arial','',15);
    // Move to the right
    $pdf->Cell(80);
    // Title
    $pdf->Cell(30,10,'Module '.$today,0,0,'C');
    $pdf->Ln(10);
    // Line break
    $pdf->Ln(20);

    $pdf->SetTextColor(0,0,0,1);
    $w = $pdf->GetStringWidth($contact)+60;
    $pdf->SetX((170-$w)/2);
    $pdf->Cell(40, 10, 'Name:', 0, 0, 'L');
    $pdf->Cell($w, 10, $sname, 0, 1, 'R');
    
    $pdf->Ln(10);

    $pdf->SetX((170-$w)/2);
    $pdf->Cell(40, 10, 'Birth Date:', 0, 0, 'L');
    $pdf->Cell($w, 10, $birthdate, 0, 1, 'R');

    $pdf->Ln(10);

    $pdf->SetX((170-$w)/2);
    $pdf->Cell(40, 10, 'Telephone:', 0, 0, 'L');
    $pdf->Cell($w, 10, $contact, 0, 1, 'R');

    $pdf->Ln(10);

    $pdf->SetX((170-$w)/2);
    $pdf->Cell(40, 10, 'Email:', 0, 0, 'L');
    $pdf->Cell($w, 10, $emailid, 0, 1, 'R');
    
    $pdf->Ln(10);

    $pdf->SetX((170-$w)/2);
    $pdf->Cell(40, 10, 'Date of Join:', 0, 0, 'L');
    $pdf->Cell($w, 10, $joindate, 0, 0, 'R');

    $pdf->Ln(30);
    
    $pdf->SetX((170-$w)/2);
    $pdf->Cell(40, 10, '', 0, 0, 'R');
    $pdf->Cell($w, 10, 'Signature:______________', 0, 0, 'R');
    
    $pdf->Output('I',"Module {$today} of {$sname}.pdf");
}

推荐阅读