Latex转换之PDF
近期一直在做如何使用latex将模板转换成PDF.现在写下在项目中如何实现。
1.首先你先进官网下载http://www.miktex.org/download。我用的是如下图所示。

在下载好的MikTeX找到如下目录(E:\Software)\MiKTeX 2.9\miktex\bin\x64中的miktex-xetex.
2.自己新建一个模板.tex文件和一个.bat文件。其中在.bat文件中写入miktex-2.9.4813\miktex\bin\miktex-xetex.exe -undump=xelatex test.tex(这是你自己所写的模板文件)。
下面是我自己项目中的一个模板,我的.tex文件如下
%!Tex Program = xelatex
\documentclass[a4paper]{article}
\usepackage{xltxtra}
\setmainfont[Mapping=tex-text]{Microsoft YaHei}
\begin{document}\pagestyle{empty}
\section{Unicode support} \subsection{English}
All human beings are born free and equal in dignity and rights. \subsection{Íslenska}
Hver maður er borinn frjáls og jafn öðrum að virðingu og réttindum. \subsection{Русский}
Все люди рождаются свободными
и равными в своем достоинстве и
правах. \subsection{Tiếng Việt}
Tất cả mọi người sinh ra đều được tự do và bình đẳng về nhân phẩm và
quyền lợi. \subsection{简体中文}
你好!!!!!
\subsection{繁體中文}
每個人生來平等,享有相同的地位和權利。 \subsection{日本語}
すべての人間は自由であり、かつ、尊厳と権利とについて平等である。 \section{Legacy syntax}
When he goes---``Hello World!''\\
She replies—“Hello dear!” \section{Ligatures}
\fontspec[Ligatures={Common, Historical}]{Times New Roman Italic}
\fontsize{12pt}{18pt}\selectfont Questo è strano assai! \section{Numerals}
\fontspec[Numbers={OldStyle}]{Times New Roman}Old style: 1234567\\
\fontspec[Numbers={Lining}]{Times New Roman}Lining: 1234567 \end{document}
3.双击.bat文件进行运行,运行结果如下。

其中调用xetex.cmd口令程序代码如下。
public static bool GenLatexPdf(string exeFile, string texString, string outPath, out string pdfUrl, out string error)
{
pdfUrl = null;
error = null; if (string.IsNullOrWhiteSpace(exeFile) || !File.Exists(exeFile)) // Exe file is not exist
{
error = string.Format("Miktex file is not exist: {0}", exeFile);
LogMethod.WriteLog(LogMethod.LogType.Error, error);
return false;
}
if (string.IsNullOrWhiteSpace(texString)) // Tex string is empty
{
error = "Tex string is empty.";
LogMethod.WriteLog(LogMethod.LogType.Error, error);
return false;
}
if (string.IsNullOrWhiteSpace(outPath)) // Output path is empty
{
error = "Output path is empty.";
LogMethod.WriteLog(LogMethod.LogType.Error, error);
return false;
} var now = DateTime.Now;
outPath = string.Format(OutputPathFormat, outPath.TrimEnd('\\'), now);
if (!Directory.Exists(outPath))
{
try
{
Directory.CreateDirectory(outPath);
}
catch (IOException e) // Fail to create directory
{
error = string.Format("Fail to create directory: {0}", outPath);
LogMethod.WriteLog(LogMethod.LogType.Error, error);
return false;
}
} string filename;
string texPath, pdfPath;
do
{
filename = Guid.NewGuid().ToString("D");
texPath = string.Format(TexFileFormat, outPath, filename);
pdfPath = string.Format(PdfFileFormat, outPath, filename);
} while (File.Exists(texPath) || File.Exists(pdfPath));
pdfUrl = string.Format(PdfUrlFormat, now, filename); // Write TEX LogMethod.WriteLog(LogMethod.LogType.Info, string.Format("Writing tex file: {0}", texPath));
try
{
using (var texWriter = new StreamWriter(new FileStream(texPath, FileMode.Create, FileAccess.Write)))
{
texWriter.WriteLine(texString);
texWriter.Flush();
texWriter.Close();
}
}
catch (IOException e) // Fail to write tex file
{
error = string.Format("Fail to write tex file: {0}", texPath);
LogMethod.WriteLog(LogMethod.LogType.Error, error);
return false;
} // Gen PDF try
{
var cmd = new Process
{
StartInfo =
{
FileName = exeFile,
Arguments = string.Format(ArgumentFormat, outPath, filename),
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden
}
};
cmd.Start();
var output = cmd.StandardOutput.ReadToEnd().Replace("\r\n", "");
var parten = @"Output written on " + pdfPath.Replace(@"\", "/") + @" \(\d+ page\)\.";
// Check if processing is not successfull
if (!Regex.IsMatch(output, parten))
{
error = string.Format("Fail to gen pdf file: {0}", texPath);
LogMethod.WriteLog(LogMethod.LogType.Error, error);
return false;
} // Delete unused file
foreach (var ext in new[] { ".tex", ".aux", ".log" })
{
var tmpFilePath = string.Format(FileFormatWithoutExtension, outPath, filename) + ext;
try
{
File.Delete(tmpFilePath);
}
catch (IOException e)
{
}
}
}
catch (Exception e)
{
error = string.Format("Fail to gen pdf file: {0}", texPath);
LogMethod.WriteLog(LogMethod.LogType.Error, error);
return false;
} return true;
}
}
在其中如果有什么问题,请留言很高兴为你解答。相互提高靠的就是你的一份真诚。本文原创,请尊重版权谢谢。
Latex转换之PDF的更多相关文章
- dvi文件和将dvi文件转换成pdf格式
dvi文件和将dvi文件转换成pdf格式 Latex只能把tex文件编译成dvi文件, 在cmd 中: 使用xdvi查看dvi格式的文件 若用texstudio编辑tex文件,则可直接将已编译成功的. ...
- C#将Word转换成PDF方法总结(基于Office和WPS两种方案)
有时候,我们需要在线上预览word文档,当然我们可以用NPOI抽出Word中的文字和表格,然后显示到网页上面,但是这样会丢失掉Word中原有的格式和图片.一个比较好的办法就是将word转换成pdf,然 ...
- ASP.Net中实现上传过程中将文本文件转换成PDF的方法
iTextSharp是一个常用的PDF库,我们可以使用它来创建.修改PDF文件或对PDF文件进行一些其他额外的操作.本文讲述了如何在上传过程中将文本文件转换成PDF的方法. 基本工作 在开始之前,我们 ...
- C# 将PowerPoint文件转换成PDF文件
PowerPoint的优势在于对演示文档的操作上,而用PPT查看资料,反而会很麻烦.这时候,把PPT转换成PDF格式保存,再浏览,不失为一个好办法.在日常编程中和开发软件时,我们也有这样的需要.本文旨 ...
- ABBYY如何把图片转换成pdf格式
在制作工作文件的时候,有时候会遇到需要进行文件格式转换的情况,比较常见的文件格式转换就包含了Office与pdf格式之间的转换.但除此之外,图片与pdf格式也是可以进行转换的,那么图片要怎么操作,才能 ...
- ASP.NET将word文档转换成pdf的代码
一.添加引用 using Microsoft.Office.Interop.Word; 二.转换方法 1.方法 C# 代码 /// <summary> /// 把Word文件转换成pdf文 ...
- word ppt excel文档转换成pdf
1.把word文档转换成pdf (1).添加引用 using Microsoft.Office.Interop.Word; 添加引用 (2).转换方法 /// <summary> /// ...
- 服务器端打开office然后采用虚拟打印 转换成pdf
服务器端打开office然后采用虚拟打印 转换成pdf [WebMethod] public bool ConvertWordTOPDF(string WordPath) { bool ret=fal ...
- 使用Spire PDF for .NET将HTML转换成PDF文档
目录 开发环境说明 Spire PDF for .NET (free edition)体验 资源下载 开发环境说明 Microsoft Visual Studio 2013 Ultimate Edit ...
随机推荐
- js之oop <二> 对象属性
js中对象属性可以动态添加和删除.删除对象属性用delete关键字. function obj(){ } var oo = new obj(); oo.a = "a"; oo.b ...
- Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用
#!/usr/bin/Python指定用什么解释器运行脚本以及解释器所在的位置 # -*- coding: utf-8 -*-用来指定文件编码为utf-8的 估计有不少人注意过一些python脚本开头 ...
- Windows 域(domain)
http://baike.baidu.com/view/1512519.htm http://baike.baidu.com/view/1218493.htm http://www.jb51.net/ ...
- text属性
-------------------------------------------------------------------------------- 对p标签进行样式的设置 text-ju ...
- 【matlab】查看程序运行时间
程序开头 profile on 结尾 profile viewer 然后就会很贴心滴出现下面的界面,可以从中展开,查看每段运行的时间
- Spring Boot工程发布到Docker
先聊聊闲话 搞过企业级的application运维的同仁肯定深有感触,每个application的功能交叉错杂,数据交换就让人焦头烂额(当然这和顶层业务设计有关系), 几十个application发布 ...
- 字符串去空格 java , js和Jquery 方法
1. java方式 String.trim(); 2.js方式 function Trim(str) { return str.replace(/(^\s*)|(\s*$)/g, "&qu ...
- 利用jsoup进行模拟登录
因为工作的原因,近段时间开始接触jsoup.大概也弄清了用java来爬网页是怎样一个过程.特此,写篇日志以便他日方便查看. Jsoup是一个java平台的能够对xml文档结构的文档进行解析.有点类似于 ...
- 【nginx】利用yum源安装nginx
先安装nginx的yum源 http://nginx.org/en/linux_packages.html#stable 找到链接,安装: rpm -ivh http://nginx.org/pack ...
- windows上安装numpy,scipy
题外话:本来按照python官方的设计,可以直接使用easy_install或者pip在线安装各个组件,但是国内的网络你懂得!老老实实下载文件本地安装吧. 1.安装windows 的python 2, ...