gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 02:52:34 25 4
gpt4 key购买 nike

我有一个像JsFiddle这样的html页面我想将其转换为 pdf,我无法创建行到行 pdf,因为页面是动态创建的,我使用 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 作为表。有人告诉我一种渲染页面的方法,如果可能的话,无需在服务器上安装软件。

最佳答案

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

有一个来自 cvision 的推荐;我没有代码示例,但您可以免费下载,甚至可以在线试用。

还有一个 php-pdf 产品可从 muhimbi 获取。 (有点不太为人所知!但我认为它是免费的)

<?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' (有依赖关系)

关于php - 如何在php中将页面转换为pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38308024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com