如何在php中将页面转换为pdf

时间:2016-07-11 13:18:04

标签: php jquery tcpdf jspdf mpdf

我有一个像JsFiddle这样的html页面,我想用pdf转换这个,我不能创建行到pdf的行,因为页面是dinamically创建的,我使用php来调用连接到mysql的fiel并填写模板文件,如。

$_POST['IdQuestionario']=5;
$_POST['IdUtente']=10001;
$_POST['Visualizza']=true;
$_POST['IdImpianto']=1;
$_POST['Stampa']=true;
$_POST['TipoImpianto']='grande';

ob_start();
ob_clean();
require_once 'intro.php';
$tbl=ob_get_clean();
$html.=$tbl;

我正在尝试使用tcpf,mpdf,jsPDF,但我无法获得离散输出,因为我使用colgroup表。有人说我是一个渲染页面的方法,如果可以在服务器上安装whitout软件。

3 个答案:

答案 0 :(得分:2)

我知道有一些 - 有些表有问题,我会避免DOMPDF - 表的已知问题。

有一个是cvision推荐的;我没有代码示例,但您可以免费下载,甚至可以在线进行采样。

还有muhimbi提供的php-pdf产品(有点鲜为人知!但我觉得它是免费的)

<?php
// Include the generated proxy classes
require_once "documentConverterServices.php";
// Check the uploaded file
if ($_FILES["file"]["error"] > 0)
{
    echo "Error uploading file: " . $_FILES["file"]["error"];
}
else 
{
    // Get the uploaded file content
    $sourceFile = file_get_contents($_FILES["file"]["tmp_name"]);

    // Create OpenOptions
    $openOptions = new OpenOptions();
    // set file name and extension
    $openOptions->FileExtension = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);
    $openOptions->OriginalFileName = $_FILES["file"]["name"];
    // Create conversionSettings
    $conversionSettings = new ConversionSettings();
    // Set the output format
    if(isset($_POST["outputFormat"]))
    {
        $conversionSettings->Format = $_POST["outputFormat"];
    } else {
        $conversionSettings->Format = "PDF";
    }
    // Set fidelity
    $conversionSettings->Fidelity = "Full";
    // These values must be set to empty strings or actual passwords when converting to non PDF formats
    $conversionSettings->OpenPassword="";
    $conversionSettings->OwnerPassword="";
    // Set some of the other conversion settings. Completely optional and just an example
    $conversionSettings->StartPage = 0;
    $conversionSettings->EndPage = 0;
    $conversionSettings->Range = "VisibleDocuments";
    $conversionSettings->Quality = "OptimizeForPrint";
    $conversionSettings->PDFProfile = "PDF_1_5";
    $conversionSettings->GenerateBookmarks = "Automatic";
    $conversionSettings->PageOrientation="Default";
    // Create the Convert parameter that is send to the server
    $convert = new Convert($sourceFile, $openOptions, $conversionSettings);
    // Create the service client and point it to the correct Conversion Service
    $url = "http://localhost:41734/Muhimbi.DocumentConverter.WebService/?wsdl";
    $serviceClient = new DocumentConverterService(array(), $url);

    // If you are expecting long running operations then consider longer timeouts
    ini_set('default_socket_timeout', 60);

    try 
    {
        // Execute the web service call
        $result = $serviceClient->Convert($convert)->ConvertResult;
        // Send the resulting file to the client.
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=\"convert." . $conversionSettings->Format . "\"");
        echo $result;
    }
    catch (Exception $e) 
    {
        print "Error converting document: ".$e->getMessage();
    }    
}
?>

此外,您可以调查'Snappy'(有依赖关系)

答案 1 :(得分:2)

您可以尝试WKHTMLTOPDF

这是关于如何在PHP中使用它的Stackoverflow线程。

How do I get WKHTMLTOPDF to execute via PHP?

这是PHP的包装器 https://github.com/mikehaertl/phpwkhtmltopdf

答案 2 :(得分:1)

MPDF转换pdf的最佳库之一,试试吧

MPDF链接:http://www.mpdf1.com/mpdf/index.php

示例链接:http://mpdf1.com/common/mpdf/examples/

相关问题