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 ...
随机推荐
- 深入浅出设计模式——策略模式(Strategy Pattern)
模式动机 完成一项任务,往往可以有多种不同的方式,每一种方式称为一个策略,我们可以根据环境或者条件的不同选择不同的策略来完成该项任务.在软件开发中也常常遇到类似的情况,实现某一个功能有多个途径,此时可 ...
- 使用自己的CSS框架(转)
[经典推介]CSS框架选择向导 不少CSS框架已经存在了一段时间,但大多数Web开发人员避免使用它们. 相反最有经验的开发者希望创建自己的CSS框架,提供个性化解决方案的优势,并减少对第三方的解决方案 ...
- ruby 资源收集
http://huacnlee.com/blog/archives/ 一个牛人李华顺的个人网站 http://api.rubyonrails.org/ 不错的ruby on rails学习网站 htt ...
- Div的宽度与高度的100%设定
div的100%是从其上一级div的宽高继承来的,所以必须设置其上一级div的宽度或高度,否则无效. 举例说明:父div(deman)宽300高200,子div(cc)如果在这个条件下设置divcc的 ...
- nodejs的第一天学习笔记
一. js的模块化 什么是模块化: 模块化的概念最早是后台,随着ajax技术的兴起,js在编程中所占的地位越来越高,同时js的文件也相应的越来越多.为了方便文件的管理和更新,提出了js文件的模块 化, ...
- js String对象中常用方法小结(字符串操作)
1.charCodeAt方法返回一个整数,代表指定位置字符的Unicode编码. strObj.charCodeAt(index) 说明: index将被处理字符的从零开始计数的编号.有效值为0到字符 ...
- Apriori原理与实践
Apriori: 其核心思想是通过候选集生成和情节的向下封闭检测两个阶段来挖掘频繁项集.经典的关联规则数据挖掘算法Apriori 算法广泛应用于各种领域,通过对数据的关联性进行了分析和挖掘,挖掘出的这 ...
- Java 内存分配全面浅析
本文将由浅入深详细介绍Java内存分配的原理,以帮助新手更轻松的学习Java.这类文章网上有很多,但大多比较零碎.本文从认知过程角度出发,将带给读者一个系统的介绍. 进入正题前首先要知道的是Java程 ...
- UNIX 逐字符输入
//终端驱动处于普通胡一次一字符模式 system("stty raw"); //终端驱动处于普通胡一次一行模式 system("stty cooked");
- 随机抽奖 --java
使用Math.random() 1.Math.random() 返回double类型. /** * 随机得到获奖名单 * @param assocs * @param prizeNumber * @r ...