需要nuget安装Aspose.PDF插件,本文使用23.10.0版本
一、获取PDF文件,通过Aspose.Pdf.Document 以Html格式 保存到某个路径;再读取该html返回字符串。
//html文件保存路径
string filePath = dirPath + "xxx.html";
if (!File.Exists(filePath))
{
//获取pdf文件流
Byte[] pdfByte = ……;
var document = new Aspose.Pdf.Document(new MemoryStream(pdfByte));
document.Save(filePath, SaveFormat.Html);
}
//读取刚刚保存的文件
string file = File.ReadAllText(filePath);
string oldPath = "xxx_files";
string newPath = ConfigurationManager.AppSettings["NurseHtmlIP"] + "/" + oldPath;
//剔除版权文字;替换本地css和图片路径 为 http路径,使得在第三方接口可调用
string targetHtml = file.Replace("Evaluation Only. Created with Aspose.PDF. Copyright 2002-2023 Aspose Pty Ltd.","").Replace(oldPath, newPath);
return targetHtml;
二、在用Base64加密,避免格式错误。
string base64Html = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(targetHtml)));
三、前端显示,js方法。
//base64转字符串
function base64ToString(str) {
return decodeURIComponent(atob(str).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
}
var showInfo = function (htmlStr) {
//把html显示到前端
$("#ele_target").html(base64ToString(htmlStr));
}
四、扩展:windows使用bat脚本删除临时文件(配合windows定时任务)
注意:有中文的路径,bat文件需要用ANSI编码
:: 删除文件夹内所有文件和文件夹
:: 打开日志打印
@echo on
:: 目标文件目录
set log_dir="C:\Windows\Temp\CustomTempFonts"
:: 保留日志天数
set bak_dat=0
:: 删除日志文件;*.txt匹配txt文件,也可用*删除所有文件;del /S /Q @file表示不经过确认强制删除文件
forfiles /p %log_dir% /S /M *.txt /D -%bak_dat% /C "cmd /c echo [delete operation @relpath file.....] & echo. & del /S /Q @file"
:: 进入文件夹A目录
cd %log_dir%
:: 对文件夹进行遍历,删除文件夹
for /D %%i in (*) DO (
echo 删文件夹:%%i
rd /S /Q %%i
)
:: 等待输入结束
pause
本文介绍了如何使用Aspose.PDF库将PDF文件转换为HTML格式,保存到本地并进行版权文字移除,然后对HTML进行Base64编码以便前端显示。

被折叠的 条评论
为什么被折叠?



