近期一直在做如何使用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. 《BI那点儿事》三国数据分析系列——蜀汉五虎上将与魏五子良将武力分析,绝对的经典分析

    献给广大的三国爱好者们,希望喜欢三国的朋友一起讨论,加深对传奇三国时代的了解 数据分析基础概念:集中趋势分析是指在大量测评数据分布中,测评数据向某点集中的情况.总体(population)是指客观存在 ...

  2. 【转】 void与void*详解

    void关键字的使用规则: 1. 如果函数没有返回值,那么应声明为void类型: 2. 如果函数无参数,那么应声明其参数为void: 3. 如果函数的参数可以是任意类型指针,那么应声明其参数为void ...

  3. Gson手动序列化POJO(工具类)

    gson2.7版本 只是简单的工具类(练习所用): package pojo; import javax.xml.bind.annotation.XmlSeeAlso; import com.goog ...

  4. web组件

    慕课网:http://www.imooc.com/learn/99 js 在实战中的分层: 应用层 框架组件-框架通用组件.定制组件.独立组件 框架core(框架核心层) 浏览器底层

  5. mysql 中关于周和月份的表示

    本周:YEARWEEK(date_format(create_time,'%Y-%m-%d')) = YEARWEEK(now()) 上周:YEARWEEK(date_format(create_ti ...

  6. angularJS 报错: [ngModel:numfmt] http://errors.angularjs.org/1.4.1/ngModel/numfmt?p0=333

    <!doctype html> <html ng-app="a10086"> <head> <meta charset="utf ...

  7. H5横向三栏布局

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

  8. 云端 Linux下安装 Java

    根据需要在给云端的机器安装Java 问题的分解为2个一个是Java源文件的传输.一个是Linux下Java的安装. Java 传输到云端: 可以通过SSH Secure File Transfer C ...

  9. iOS开发多线程篇—线程间的通信

    iOS开发多线程篇—线程间的通信 一.简单说明 线程间通信:在1个进程中,线程往往不是孤立存在的,多个线程之间需要经常进行通信 线程间通信的体现 1个线程传递数据给另1个线程 在1个线程中执行完特定任 ...

  10. Rhel6-sersync配置文档

    系统环境: rhel6 x86_64 iptables and selinux disabled 主服务器: 192.168.122.160 server60.example.com 目标服务器: 1 ...