近期一直在做如何使用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的更多相关文章

  1. dvi文件和将dvi文件转换成pdf格式

    dvi文件和将dvi文件转换成pdf格式 Latex只能把tex文件编译成dvi文件, 在cmd 中: 使用xdvi查看dvi格式的文件 若用texstudio编辑tex文件,则可直接将已编译成功的. ...

  2. C#将Word转换成PDF方法总结(基于Office和WPS两种方案)

    有时候,我们需要在线上预览word文档,当然我们可以用NPOI抽出Word中的文字和表格,然后显示到网页上面,但是这样会丢失掉Word中原有的格式和图片.一个比较好的办法就是将word转换成pdf,然 ...

  3. ASP.Net中实现上传过程中将文本文件转换成PDF的方法

    iTextSharp是一个常用的PDF库,我们可以使用它来创建.修改PDF文件或对PDF文件进行一些其他额外的操作.本文讲述了如何在上传过程中将文本文件转换成PDF的方法. 基本工作 在开始之前,我们 ...

  4. C# 将PowerPoint文件转换成PDF文件

    PowerPoint的优势在于对演示文档的操作上,而用PPT查看资料,反而会很麻烦.这时候,把PPT转换成PDF格式保存,再浏览,不失为一个好办法.在日常编程中和开发软件时,我们也有这样的需要.本文旨 ...

  5. ABBYY如何把图片转换成pdf格式

    在制作工作文件的时候,有时候会遇到需要进行文件格式转换的情况,比较常见的文件格式转换就包含了Office与pdf格式之间的转换.但除此之外,图片与pdf格式也是可以进行转换的,那么图片要怎么操作,才能 ...

  6. ASP.NET将word文档转换成pdf的代码

    一.添加引用 using Microsoft.Office.Interop.Word; 二.转换方法 1.方法 C# 代码 /// <summary> /// 把Word文件转换成pdf文 ...

  7. word ppt excel文档转换成pdf

    1.把word文档转换成pdf (1).添加引用 using Microsoft.Office.Interop.Word; 添加引用 (2).转换方法 /// <summary> /// ...

  8. 服务器端打开office然后采用虚拟打印 转换成pdf

    服务器端打开office然后采用虚拟打印 转换成pdf [WebMethod] public bool ConvertWordTOPDF(string WordPath) { bool ret=fal ...

  9. 使用Spire PDF for .NET将HTML转换成PDF文档

    目录 开发环境说明 Spire PDF for .NET (free edition)体验 资源下载 开发环境说明 Microsoft Visual Studio 2013 Ultimate Edit ...

随机推荐

  1. web过滤器中获取请求的参数(content-type:multipart/form-data)

    1.前言: 1.1 在使用springMVC中,需要在过滤器中获取请求中的参数token,根据token判断请求是否合法: 1.2 通过requst.getParameter(key)方法获得参数值; ...

  2. JDBC连接sql server数据库及其它

    JDBC连接sql server数据库的步骤如下: 1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机), 这通过java.lang.Class类的 ...

  3. html之meta详解

    <!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --> <html lang="zh-cmn-Hans"&g ...

  4. Oracle10g_Dataguard__161031

    1.查看  redo log 信息 1.1.desc v$log ZC: 不明白这是查看什么信息... 1.2. 查看redo log file SQL> select * from v$log ...

  5. 根据屏幕的宽度使用不同的css-文件

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  6. [cocos2d-x]OPENGL ES支持的像素格式

    OPENGL ES最多支持32位颜色值. 支持的像素格式有以下几种: 客户端格式 GL格式 GL数据类型 字节数 RGBA8888 GL_RGBA GL_UNSIGNED_BYTE 4 RGB888 ...

  7. CSS权重及样式优先级问题

    CSS权重值计算 一条样式规则的整体权重值包含四个独立的部分:[A, B, C, D]; (1) A 表示内联样式(写在标签的style属性中),只有 1 或者 0 两个值:对于内联样式,由于没有选择 ...

  8. 解决html中&nbsp;在不同浏览器中占位大小不统一的问题

    直接在html文档中使用 来表示空格,在不同浏览器中的占位大小是不一样的. 为什么呢,因为不同浏览器默认的字体是不一样的,不同字体下的空格表示 占位大小不一致. 这就好办了嘛,我们对 指定使用同样的字 ...

  9. ionic 安装本地插件极光推送

    问题:按照官方文档的步骤 假如把插件保存到了D:\push\jpush,当执行到 cordova plugin add D:\push\jpush 的时候,ionic 不是从本地目录安装,而是从reg ...

  10. win7(64)位下WinDbg64调试VMware10下的win7(32位)

    win7(64)位下WinDbg64调试VMware10下的win7(32位) 一 Windbg32位还是64位的选择 参考文档<Windbg 32位版本和64位版本的选择> http:/ ...