【问题标题】:how to convert page to pdf in php如何在php中将页面转换为pdf
【发布时间】:2016-11-13 11:08:50
【问题描述】:

我有一个像 JsFiddle 这样的 html 页面,我想将其转换为 pdf,我无法创建行到行 pdf,因为该页面是动态创建的,我使用 php 调用连接到 mysql 并填充的字段类似的模板文件。

$_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 作为表格。任何人都说我一种渲染页面的方法,如果可能的话,可以在服务器上安装软件。

【问题讨论】:

  • 你试过fpdf.org 吗?你也可以插入html
  • 我试了但是文件格式不好
  • 你能发布你使用的完整代码吗?
  • 简单我有很多模板文件,为了创建在 jsfiddle 中查看的页面,我调用了一个 mysql 数据库并通过像 $_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; 这样的导入填充模板

标签: php jquery tcpdf jspdf mpdf


【解决方案1】:

我知道一些 - 有些表格有问题,我会避免使用 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'(有依赖关系)

【讨论】:

  • tcpdf.org - 目前不是免费的,但很快就会免费(新版本正在 github 上开发。docraptor.com - 也付费,但免费试用。
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

    你可以试试WKHTMLTOPDF

    这是一个 Stackoverflow 线程,介绍如何将它与 PHP 一起使用。

    How do I get WKHTMLTOPDF to execute via PHP?

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-01
      • 2012-05-28
      • 2021-12-12
      相关资源
      最近更新 更多