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 ...
随机推荐
- Shell脚本查看apk签名信息
用shell写了一个查看apk签名的脚本.代码很少也很简单 支持递归目录查询 #!/bin/bash #使用方法 ./getcertificate.sh xx.apk get_signature() ...
- HDU-4532 湫秋系列故事——安排座位 组合数学DP
题意:有来自n个专业的学生,每个专业分别有ai个同学,现在要将这些学生排成一行,使得相邻的两个学生来自不同的专业,问有多少种不同的安排方案. 分析:首先将所有专业的学生视作一样的,最后再乘以各自学生的 ...
- iOS - iOS 适配
前言 什么是适配: 适应.兼容各种不同的情况. iOS 开发中,适配的常见种类: 1)系统适配, 针对不同版本的操作系统进行适配. 2)屏幕适配,针对不同大小的屏幕尺寸进行适配. iPhone 的尺寸 ...
- hdu2297Run(凸包的巧妙应用)
链接 很巧妙的一道题,参考 把距离和速度分别作为x和y坐标,以斜率代表追赶速率,简直炫酷~ 具体看上面的博客,画的很清楚,就不再抄写一遍了. #include <iostream> #in ...
- Python学习(14)模块二
一:_name_ Python中if _name_ == '_main_'的解析 当你打开一个.py文件时,经常会在代码的最下面看到if __name__ == '__main__':,现在就来介 绍 ...
- 五大权限:UGO权限、SetUID SetGID Sticky、ACL权限、chattr(文件系统级别的权限)、SELINUX
五大权限:UGO权限.SetUID SetGID Sticky.ACL权限.chattr(文件系统级别的权限).SELINUX ======================文件属性以及ugo权限= ...
- Windows下使用xShell向远程Linux上传文件
上传文件使用rz与sz命令,远程Linux系统上需要安装lrzsz工具包 下载安装包lrzsz-0.12.20.tar.gz: http://www.linuxidc.com/Linux/2010- ...
- .NET 4.5 WPF Ribbon
文/嶽永鹏 Visual Studio 2012 DO.NET 4.5 Ribbon 界面编程. 代码 =============================================== ...
- springmvc单文件上传
1.创建上传页面 <form action="first.do" method="post" enctype="multipart/form-d ...
- oracle修改序列
Oracle 序列(Sequence)主要用于生成流水号,在应用中经常会用到,特别是作为ID值,拿来做表主键使用较多. 但是,有时需要修改序列初始值(START WITH)时,有同仁使用这个语句来 ...