我想创建一个PHP脚本,将XML格式化为TV/,这比预期的要困难一些。我已经创建了一些代码,但是,在某些特定的地方,我会陷入困境。
一个小小的背景
在一个剧本中,有多个标签,如字符,对话框,浏览标题,转换等等。它们有不同的宽度,左右边距决定了页面上的位置。
有两个主要方面引起了关注,即我无法弄清楚:
无法分离的
)
但是,理想情况下,应该将父/子标记存储在一起,这样就可以在不需要大量代码的情况下添加更多的标记。
在进行了大量的FPDF配置和尝试之后,以下是我到目前为止所做的工作:
// Parse the XML string which has been requested from the database.
$xml = new SimpleXmlIterator($file, null);
$namespaces = $xml->getNamespaces(true);
/*
* An external function which converts the XML into an ORDERED array.
* This returns all the attributes of the xml and the following:
* "@type" Refers to the type, i.e. character/dialog etc,
* "@content" What was actually within the tags of the XML.
*/
$source = $parseXML($xml, $namespaces);
$saved = array ();
// For every line of the XML.
foreach ($source as $index => $line) {
/*
* We are going to save the current line of the XML so that we
* can check the size of the saved cells against the size of the
* page. If there is not enough space for specific tags which are
* required such as character and dialog, then we create a new page.
*
*/
$saved[] = $line;
$forward = false;
/*
* Here is where I get somewhat confused - I have attempted to create
* a mechanism that determines the "@types" which are needed as parents
* and children of one another.
*
* The $forward variable refers to the possibilty of writing the tag to
* the PDF. If the parent/orphan is possible or there are non, then it
* should be able to write.
*/
if ($forward) {
$width = 0;
foreach ($saved as $index => $value) {
/*
* Everything is measured in inches, therefore, I have created a
* point() function which turns inches into mm so that FPDF understands
* everything.
*
* The $format variable is an array which has margins, either top,
* left or right. We use this to position the Cell of a specific @type
* so that it appears in the correct format for a TV/Film script.
*
* The width variable is deduced via finding the full width of the page,
* then subtracting the subsequent left and right margin of the said
* @type.
*/
$width = (point ($setting["width"])
- (point ($format[$value["@type"]]["margin-left"])
+ point ($format[$value["@type"]]["margin-right"])));
/*
* The $pdf variable is that of the FPDF Class.
*/
$pdf->SetLeftMargin (point($format[$value["@type"]]["margin-left"]));
$pdf->SetRightMargin (point($format[$value["@type"]]["margin-right"]));
// Formatting purposes.
$pdf->Ln (0);
$pdf->MultiCell (
$width,
point ($setting["line-height"]), //Line height? Still needs fixing.
$value["@content"], // Physical text
1); // A temporary border to see what's going on.
}
// Reset the $saved, after no more parent/children?
$saved = array ();
}
}
此外,我还编写了一个函数来计算合并的"$saved“行的高度:
function calculate_page_breaks ($saved_lines, $act_upon = true) {
$num_lines = 0;
foreach ($saved_lines as $value) {
$column_width = (point ($setting["width"])
- (point ($format[$type][$value["@type"]]["margin-left"])
+ point ($format[$type][$value["@type"]]["margin-right"])));
$num_lines += ceil($pdf->GetStringWidth($value["@content"]) / ($column_width - 1));
}
$cell_height = 5 + 1; // have cells at a height of five so set to six for a padding
$height_of_cell = ceil($num_lines * $cell_height);
$space_left = point($setting["height"]) - $pdf->GetY();
$space_left -= point($format[$type]["margin-bottom"]);
// test if height of cell is greater than space left
if ($height_of_cell >= $space_left) {
// If this is not just a check, then we can physically "break" the page.
if ($act_upon) {
$pdf->Ln ();
$pdf->AddPage (); // page break
$pdf->SetTopMargin ($point($format[$type]["margin-top"]));
$pdf->SetLeftMargin ($point($format[$type]["margin-left"]));
$pdf->SetRightMargin ($point($format[$type]["margin-right"]));
$pdf->MultiCell (100,5,'','B',2); // this creates a blank row for formatting reasons
}
return false;
} else {
return true;
}
}
编辑
我发现LaTeX提供了一个屏幕播放类( Screenplay,http://www.tug.org/texlive/Contents/live/texmf-dist/doc/latex/screenplay/screenplay.pdf),但是,我搜索了很远的地方,找到了一个基于PHP的工具来将LaTeX转换成PDF格式,但是没有成功。我知道LaTeX在服务器端运行,但是我仍然需要一个基于PHP的命令过程,以便使用LaTeX生成上述PDF文件。
此外,在服务器上安装二进制文件、库或工具是不可能的。我可以使用的工具是PHP及其内置的功能。任何可以将LaTex转换为PDF的类或PHP工具都是非常有用的。
发布于 2012-01-03 15:47:36
根据您的要求,我建议使用联机web服务来推送您的LateX文件并获得编译的PDF作为回报。
这意味着你得到一个完整的PDF文件,没有任何编译,没有安装,只是纯粹的基于PHP的 web服务和API。
有许多在线解决方案,有些是免费的,所以请查看适合您需要的解决方案:Compiling documents online
祝你好运!
https://stackoverflow.com/questions/8478720
复制相似问题