Latex生成PDF
This commit is contained in:
@@ -14,7 +14,7 @@ class LatexContent{
|
|||||||
//模版地址
|
//模版地址
|
||||||
private $sLatexUrl = '/public/latex/';
|
private $sLatexUrl = '/public/latex/';
|
||||||
//期刊官网接口地址
|
//期刊官网接口地址
|
||||||
private $sJournalUrl = 'http://zmzm.journal.dev.com/';//'http://journalapi.tmrjournals.com/public/index.php/';//
|
private $sJournalUrl = 'http://journalapi.tmrjournals.com/public/index.php/';//'http://zmzm.journal.dev.com/';//
|
||||||
//文章图片
|
//文章图片
|
||||||
private $sArticleIcon = '/public/articleicon/';
|
private $sArticleIcon = '/public/articleicon/';
|
||||||
//文章图片
|
//文章图片
|
||||||
@@ -259,6 +259,11 @@ page={{stage_page}},%号
|
|||||||
$aWhere = ['ami_id' => ['in',$aAmiId],'state' => 0];
|
$aWhere = ['ami_id' => ['in',$aAmiId],'state' => 0];
|
||||||
$aArticleMainImage = Db::name('article_main_image')->field('ami_id,url,note')->where($aWhere)->select();
|
$aArticleMainImage = Db::name('article_main_image')->field('ami_id,url,note')->where($aWhere)->select();
|
||||||
$aArticleMainImage = empty($aArticleMainImage) ? [] : array_column($aArticleMainImage, null,'ami_id');
|
$aArticleMainImage = empty($aArticleMainImage) ? [] : array_column($aArticleMainImage, null,'ami_id');
|
||||||
|
//查询表格
|
||||||
|
$aAmtId = array_unique(array_column($aArticleMain, 'amt_id'));
|
||||||
|
$aWhere = ['amt_id' => ['in',$aAmtId],'state' => 0];
|
||||||
|
$aArticleMainTable = Db::name('article_main_table')->where($aWhere)->select();
|
||||||
|
$aArticleMainTable = empty($aArticleMainTable) ? [] : array_column($aArticleMainTable, null,'amt_id');
|
||||||
//获取图片模版
|
//获取图片模版
|
||||||
$sImageTempalte = $this->sImageTempalte;
|
$sImageTempalte = $this->sImageTempalte;
|
||||||
//数据处理
|
//数据处理
|
||||||
@@ -316,16 +321,23 @@ page={{stage_page}},%号
|
|||||||
if(empty($aResult['data'])){
|
if(empty($aResult['data'])){
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$aImageSearch = ['{{img_url}}' => $aResult['data'],'{{img_title}}' => $aImageInfo['note'],'{{img_fig_sim}}' => 'img_fig_sim_'.$value['ami_id']];
|
// $aImageSearch = ['{{img_url}}' => $aResult['data'],'{{img_title}}' => $aImageInfo['note'],'{{img_fig_sim}}' => 'img_fig_sim_'.$value['ami_id']];
|
||||||
$sImageTempalteInfo = str_replace(array_keys($aImageSearch), array_values($aImageSearch), $sImageTempalte);
|
// $sImageTempalteInfo = str_replace(array_keys($aImageSearch), array_values($aImageSearch), $sImageTempalte);
|
||||||
$sMain .= $sImageTempalteInfo."\\par\n";
|
$aImageInfo['image_url'] = $aResult['data'];
|
||||||
|
$aDealImage = $this->dealImage($aImageInfo);
|
||||||
|
if(empty($aDealImage['data'])){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$sMain .= $aDealImage['data']."\\par\n";
|
||||||
}
|
}
|
||||||
if($value['type'] == 2 ){//表格
|
if($value['type'] == 2 ){//表格
|
||||||
$sTableInfo = $oProduction->tableCovertLatex($value['amt_id'],$aReferences);
|
$aTableInfo = empty($aArticleMainTable[$value['amt_id']]) ? [] : $aArticleMainTable[$value['amt_id']];
|
||||||
var_dump($sTableInfo);
|
if(empty($aTableInfo['table_data'])){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$sTableInfo = $oProduction->tableCovertLatex($value['amt_id'],$aReferences,$aTableInfo);
|
||||||
$sMain .= $sTableInfo."\\par\n";
|
$sMain .= $sTableInfo."\\par\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -549,60 +561,8 @@ page={{stage_page}},%号
|
|||||||
//字符串处理
|
//字符串处理
|
||||||
$oProduction = new \app\api\controller\Production;
|
$oProduction = new \app\api\controller\Production;
|
||||||
$sContent = $oProduction->convertToLatex($content,$aReferences);
|
$sContent = $oProduction->convertToLatex($content,$aReferences);
|
||||||
$content = $this->htmlToLaTeX($content);
|
|
||||||
return $sContent;
|
return $sContent;
|
||||||
}
|
}
|
||||||
private function htmlToLaTeX($text = ''){
|
|
||||||
if(empty($text)){
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
$replaceMap = [
|
|
||||||
// 基础标签转换
|
|
||||||
'<i>' => '\textit{',
|
|
||||||
'</i>' => '}',
|
|
||||||
'<em>' => '\textit{',
|
|
||||||
'</em>' => '}',
|
|
||||||
'<b>' => '\textbf{',
|
|
||||||
'</b>' => '}',
|
|
||||||
'<blue>' => '\textcolor{blue}{',
|
|
||||||
'</blue>' => '}',
|
|
||||||
'<p>' => '',
|
|
||||||
'</p>' => '',
|
|
||||||
'<sub>' => '$_{',
|
|
||||||
'</sub>' => '}$',
|
|
||||||
'<sup>' => '$^{',
|
|
||||||
'</sup>' => '}$',
|
|
||||||
'~' => '--', // 波浪号→范围符
|
|
||||||
'(' => '(', // 全角左括号→半角
|
|
||||||
')' => ')', // 全角右括号→半角
|
|
||||||
'((' => '(', // 重复括号去重
|
|
||||||
'))' => ')',
|
|
||||||
|
|
||||||
// ========== 新增:<span> 标签处理 ==========
|
|
||||||
// 场景1:纯空 <span> 标签(无属性)→ 直接删除,保留内容
|
|
||||||
'<span>' => '',
|
|
||||||
'</span>' => '',
|
|
||||||
// 场景2:带蓝色样式的 <span style="color:blue"> → 转为 LaTeX 蓝色命令
|
|
||||||
'<span style="color:blue">' => '\textcolor{blue}{',
|
|
||||||
'<span style="color: Blue;">' => '\textcolor{blue}{', // 兼容大小写/空格
|
|
||||||
'<span style="color:red">' => '\textcolor{red}{', // 扩展:红色
|
|
||||||
'</span>' => '}', // 所有 span 闭标签统一转为 }(匹配开标签的 LaTeX 命令)
|
|
||||||
|
|
||||||
// 扩展:其他常用 span 样式(按需添加)
|
|
||||||
'<span style="font-weight:bold">' => '\textbf{',
|
|
||||||
'<span style="font-style:italic">' => '\textit{',
|
|
||||||
];
|
|
||||||
|
|
||||||
// 第一步:替换所有标签(优先处理带属性的 span,再处理空 span)
|
|
||||||
$text = strtr($text, $replaceMap);
|
|
||||||
|
|
||||||
// 第二步:清理残留的特殊 span 标签(如未匹配的属性,直接删除标签保留内容)
|
|
||||||
// 匹配 <span ...> 形式的标签(任意属性),仅删除标签,保留内容
|
|
||||||
$text = preg_replace('/<span(\s+[^>]*?)?>/i', '', $text);
|
|
||||||
$text = preg_replace('/<\/span>/i', '', $text);
|
|
||||||
|
|
||||||
return $text;
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* @title curl 请求获取图片保存到本地
|
* @title curl 请求获取图片保存到本地
|
||||||
* @param sPath 图片链接地址
|
* @param sPath 图片链接地址
|
||||||
@@ -620,7 +580,7 @@ page={{stage_page}},%号
|
|||||||
$sExtension = empty($aImageInfo['extension']) ? 'jpg' : $aImageInfo['extension'];
|
$sExtension = empty($aImageInfo['extension']) ? 'jpg' : $aImageInfo['extension'];
|
||||||
|
|
||||||
//图片地址
|
//图片地址
|
||||||
$sDir = ROOT_PATH.trim($this->sLatexUrl,'/').'/article_pdf/'.$iId.'/';
|
$sDir = ROOT_PATH.trim($this->sLatexUrl,'/').'/tex_'.$iId.'/image/';
|
||||||
$sImagePath = $sDir.basename($sPath);
|
$sImagePath = $sDir.basename($sPath);
|
||||||
if (file_exists($sImagePath)) {
|
if (file_exists($sImagePath)) {
|
||||||
return ['status' => 1,'msg' => 'success','data' => $sImagePath];
|
return ['status' => 1,'msg' => 'success','data' => $sImagePath];
|
||||||
@@ -645,6 +605,57 @@ page={{stage_page}},%号
|
|||||||
return ['status' => 1,'msg' => 'success','data' => $sImagePath];
|
return ['status' => 1,'msg' => 'success','data' => $sImagePath];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* @title curl 图片Latex代码封装
|
||||||
|
*/
|
||||||
|
private function dealImage($aImageInfo = []){
|
||||||
|
|
||||||
|
//获取图片路径
|
||||||
|
$sImageUrl = empty($aImageInfo['image_url']) ? '' : $aImageInfo['image_url'];
|
||||||
|
if(empty($sImageUrl)){
|
||||||
|
return ['status' => 2,'msg' => 'The image link is empty'];
|
||||||
|
}
|
||||||
|
//判断图片是否存在
|
||||||
|
if(!file_exists($sImageUrl)){
|
||||||
|
return ['status' => 3,'msg' => 'The image does not exist:'.$sImageUrl];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检测图片尺寸
|
||||||
|
$imageInfo = getimagesize($sImageUrl);
|
||||||
|
$imageWidth = $imageInfo[0]; // 图片宽度
|
||||||
|
$imageHeight = $imageInfo[1]; // 图片高度
|
||||||
|
|
||||||
|
// 定义宽度阈值(例如:宽度大于600像素则使用两栏)
|
||||||
|
$wideImageThreshold = 600;
|
||||||
|
$isWideImage = $imageWidth > $wideImageThreshold;
|
||||||
|
|
||||||
|
// 生成LaTeX图片代码
|
||||||
|
if ($isWideImage) {
|
||||||
|
$latexLines[] = "\\begin{figure*}[htbp]"; // 使用figure*环境实现两栏
|
||||||
|
$latexLines[] = " \\centering";
|
||||||
|
$latexLines[] = " \\includegraphics[width=0.9\\textwidth]{" . $sImageUrl . "}"; // 使用全宽度
|
||||||
|
} else {
|
||||||
|
$latexLines[] = "\\begin{figure}[H]";
|
||||||
|
$latexLines[] = " \\centering";
|
||||||
|
$latexLines[] = " \\includegraphics[width=0.9\\textwidth]{" . $sImageUrl . "}";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if(!empty($aImageInfo['title']) && !empty($aImageInfo['note'])){
|
||||||
|
$escapedTitle = '{\fontspec{Calibri}\footnotesize\bfseries\color{figerTitleColor} '.$this->dealContent(preg_replace('/^Figure\s+\d+\s*/i', '', $aImageInfo['title']),[]).'}\\\\';
|
||||||
|
$escapedTitle .= '{\vspace{0.5em}\raggedright\small {'.$this->dealContent($aImageInfo['note'],[]).'}}';
|
||||||
|
$latexLines[] = " \\caption{" . $escapedTitle . "}";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($ami_info['title'])) {
|
||||||
|
$escapedTitle = $this->dealContent(preg_replace('/^Figure\s+\d+\s*/i', '', $ami_info['title']),[]);
|
||||||
|
$latexLines[] = " \\caption{" . $escapedTitle . "}";
|
||||||
|
}
|
||||||
|
$latexLines[] = "\\end{figure" . ($isWideImage ? "*" : "") . "}";
|
||||||
|
|
||||||
|
return ['status' => 1,'msg' => 'success','data' => implode("\n", $latexLines)];
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
Reference in New Issue
Block a user