使用Angular4和PHP生成自定义PDF服务器或客户端
我正在用PHP API构建一个Angular 4应用程序。在应用程序中,用户可以生成某种“杂志”。这允许他们对页面进行排序、编辑内容和添加图像,但不是以所见即所得的方式,而是以一步一步“我选择的这个选项就是我想要的”的方式 因此,我最终在一个MySQL数据库中存储了大量数据,类似于“描述”最终PDF的样子 问题是我完全不知道如何生成PDF。我知道有些东西像pdfmake或jsPDF作为客户端解决方案,或者tcpdf(似乎永远处于版本转换中?!)作为服务器端解决方案。但所有这些都是有限的 我认为最好的解决方案是生成一些LaTeX代码并从中生成一些PDF,因为它能够使用各种LaTeX命令,而不是jsPDF或pdfmake的有限命令 是否有任何标准或最佳方法来管理使用angular编译LaTeX代码使用Angular4和PHP生成自定义PDF服务器或客户端,php,angular,pdf,Php,Angular,Pdf,我正在用PHP API构建一个Angular 4应用程序。在应用程序中,用户可以生成某种“杂志”。这允许他们对页面进行排序、编辑内容和添加图像,但不是以所见即所得的方式,而是以一步一步“我选择的这个选项就是我想要的”的方式 因此,我最终在一个MySQL数据库中存储了大量数据,类似于“描述”最终PDF的样子 问题是我完全不知道如何生成PDF。我知道有些东西像pdfmake或jsPDF作为客户端解决方案,或者tcpdf(似乎永远处于版本转换中?!)作为服务器端解决方案。但所有这些都是有限的 我认为最
走哪条路?服务器端还是客户端?将要创建的LaTeX和PDF包含大量图像,大约100-200页 供其他人搜索 CLSI似乎是管理it的一种方式。仍有一个开源api用于编译LaTeX文件: 多亏了mike42 用PHP编译LaTeX的另一个非常有趣的方法。。。这实际上是我要走的路。。。是生成一个
.tex
文件,该文件是一个有效的LaTeX文件和一个有效的PHP文件,因此代码的结尾如下:
% This file is a valid PHP file and also a valid LaTeX file
% When processed with LaTeX, it will generate a blank template
% Loading with PHP will fill it with details
\documentclass{article}
% Required for proper escaping
\usepackage{textcomp} % Symbols
\usepackage[T1]{fontenc} % Input format
% Because Unicode etc.
\usepackage{fontspec} % For loading fonts
\setmainfont{Liberation Serif} % Has a lot more symbols than Computer Modern
% Make placeholders visible
\newcommand{\placeholder}[1]{\textbf{$<$ #1 $>$}}
% Defaults for each variable
\newcommand{\test}{\placeholder{Data here}}
% Fill in
% <?php echo "\n" . "\\renewcommand{\\test}{" . LatexTemplate::escape($data['test']) . "}\n"; ?>
\begin{document}
\section{Data From PHP}
\test{}
\end{document}
/**
* Generate a PDF file using xelatex and pass it to the user
*/
public static function download($data, $template_file, $outp_file) {
// Pre-flight checks
if(!file_exists($template_file)) {
throw new Exception("Could not open template");
}
if(($f = tempnam(sys_get_temp_dir(), 'tex-')) === false) {
throw new Exception("Failed to create temporary file");
}
$tex_f = $f . ".tex";
$aux_f = $f . ".aux";
$log_f = $f . ".log";
$pdf_f = $f . ".pdf";
// Perform substitution of variables
ob_start();
include($template_file);
file_put_contents($tex_f, ob_get_clean());
}
之后,应执行所选引擎以生成输出文件:
// Run xelatex (Used because of native unicode and TTF font support)
$cmd = sprintf("xelatex -interaction nonstopmode -halt-on-error %s",
escapeshellarg($tex_f));
chdir(sys_get_temp_dir());
exec($cmd, $foo, $ret);
// No need for these files anymore
@unlink($tex_f);
@unlink($aux_f);
@unlink($log_f);
// Test here
if(!file_exists($pdf_f)) {
@unlink($f);
throw new Exception("Output was not generated and latex returned: $ret.");
}
为所有其他人搜索 CLSI似乎是管理it的一种方式。仍有一个开源api用于编译LaTeX文件: 多亏了mike42 用PHP编译LaTeX的另一个非常有趣的方法。。。这实际上是我要走的路。。。是生成一个
.tex
文件,该文件是一个有效的LaTeX文件和一个有效的PHP文件,因此代码的结尾如下:
% This file is a valid PHP file and also a valid LaTeX file
% When processed with LaTeX, it will generate a blank template
% Loading with PHP will fill it with details
\documentclass{article}
% Required for proper escaping
\usepackage{textcomp} % Symbols
\usepackage[T1]{fontenc} % Input format
% Because Unicode etc.
\usepackage{fontspec} % For loading fonts
\setmainfont{Liberation Serif} % Has a lot more symbols than Computer Modern
% Make placeholders visible
\newcommand{\placeholder}[1]{\textbf{$<$ #1 $>$}}
% Defaults for each variable
\newcommand{\test}{\placeholder{Data here}}
% Fill in
% <?php echo "\n" . "\\renewcommand{\\test}{" . LatexTemplate::escape($data['test']) . "}\n"; ?>
\begin{document}
\section{Data From PHP}
\test{}
\end{document}
/**
* Generate a PDF file using xelatex and pass it to the user
*/
public static function download($data, $template_file, $outp_file) {
// Pre-flight checks
if(!file_exists($template_file)) {
throw new Exception("Could not open template");
}
if(($f = tempnam(sys_get_temp_dir(), 'tex-')) === false) {
throw new Exception("Failed to create temporary file");
}
$tex_f = $f . ".tex";
$aux_f = $f . ".aux";
$log_f = $f . ".log";
$pdf_f = $f . ".pdf";
// Perform substitution of variables
ob_start();
include($template_file);
file_put_contents($tex_f, ob_get_clean());
}
之后,应执行所选引擎以生成输出文件:
// Run xelatex (Used because of native unicode and TTF font support)
$cmd = sprintf("xelatex -interaction nonstopmode -halt-on-error %s",
escapeshellarg($tex_f));
chdir(sys_get_temp_dir());
exec($cmd, $foo, $ret);
// No need for these files anymore
@unlink($tex_f);
@unlink($aux_f);
@unlink($log_f);
// Test here
if(!file_exists($pdf_f)) {
@unlink($f);
throw new Exception("Output was not generated and latex returned: $ret.");
}