我想创建一个将XML格式化为电视/电影剧本的PHP脚本,事实证明这比预期的要困难一些。我创建了一些代码,然而,有些地方我遇到了问题。
一点背景
在剧本中存在多个标签,如角色,对话,场景标题,过渡等。它们具有不同的宽度,左右边距决定了页面上的位置。
有两个主要方面已经引起关注,即我无法弄清楚:
然而,理想情况下,父/子标签应该存放在一起,以便在没有大量代码的情况下添加更多标签。
这是我到目前为止所做的大量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提供了一个剧本类(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工具都非常有用。
答案 0 :(得分:0)
根据您的要求,我建议您使用在线网络服务推送您的LateX文件并获得已编译的PDF文件。
这意味着您可以获得完整的PDF文件而无需任何编译,无需安装,只需基于PHP的纯 Web服务和API。
有许多在线解决方案,有些是免费的,请检查符合您需求的解决方案:Compiling documents online
祝你好运!