1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-12-22 13:00:08 +00:00

clean tmp files

This commit is contained in:
Lu Wang 2012-08-15 12:27:41 +08:00
parent 5c2fa8ead1
commit 27d99ebcf5
8 changed files with 77 additions and 25 deletions

View File

@ -1,7 +1,6 @@
<!-- Created by pdf2htmlEX (http://github.com/coolwanglu/pdf2htmlEX) -->
<!-- head.html by WangLu 2012.08.14 -->
<!DOCTYPE html>
<!-- head.html -->
<!-- by WangLu -->
<!-- 2012.08.14 -->
<html>
<head>
<meta charset="utf-8">

View File

@ -1,3 +1,5 @@
<!-- neck.html by WangLu 2012.08.15 -->
<title></title>
<script type="text/javascript">
function show_pages()
{

View File

@ -1,6 +1,4 @@
<!-- tail.html -->
<!-- by WangLu -->
<!-- 2012.08.14 -->
<!-- tail.hml by WangLu 2012.08.15 -->
</div>
</body>
</html>

View File

@ -12,6 +12,7 @@
#include <unordered_map>
#include <map>
#include <vector>
#include <set>
#include <boost/format.hpp>
#include <boost/filesystem/fstream.hpp>
@ -81,8 +82,6 @@ class HTMLRenderer : public OutputDev
virtual void post_process();
virtual void process_single_html();
virtual boost::filesystem::path working_dir() const { return (param->single_html ? tmp_dir : dest_dir); }
// Start a page.
virtual void startPage(int pageNum, GfxState *state);
@ -106,6 +105,11 @@ class HTMLRenderer : public OutputDev
virtual void drawImage(GfxState * state, Object * ref, Stream * str, int width, int height, GfxImageColorMap * colorMap, GBool interpolate, int *maskColors, GBool inlineImg);
protected:
////////////////////////////////////////////////////
// misc
////////////////////////////////////////////////////
void add_tmp_file (const std::string & fn);
void clean_tmp_files ();
std::string dump_embedded_font (GfxFont * font, long long fn_id);
////////////////////////////////////////////////////
@ -213,6 +217,7 @@ class HTMLRenderer : public OutputDev
const Param * param;
boost::filesystem::path dest_dir, tmp_dir;
boost::filesystem::ofstream html_fout, allcss_fout;
std::set<std::string> tmp_files;
};
#endif /* HTMLRENDERER_H_ */

View File

@ -7,8 +7,6 @@
* 2012.08.14
*/
#include <iomanip>
#include <splash/SplashBitmap.h>
#include "HTMLRenderer.h"
@ -17,6 +15,8 @@
#include "namespace.h"
using std::flush;
using boost::filesystem::remove;
using boost::filesystem::filesystem_error;
HTMLRenderer::HTMLRenderer(const Param * param)
:line_opened(false)
@ -37,7 +37,9 @@ HTMLRenderer::HTMLRenderer(const Param * param)
}
HTMLRenderer::~HTMLRenderer()
{ }
{
clean_tmp_files();
}
void HTMLRenderer::process(PDFDoc *doc)
{
@ -66,7 +68,11 @@ void HTMLRenderer::process(PDFDoc *doc)
doc->displayPage(bg_renderer, i, param->h_dpi2, param->v_dpi2,
0, true, false, false,
nullptr, nullptr, nullptr, nullptr);
bg_renderer->getBitmap()->writeImgFile(splashFormatPng, (char*)(working_dir() / (format("p%|1$x|.png")%i).str()).c_str(), param->h_dpi2, param->v_dpi2);
string fn = (format("p%|1$x|.png")%i).str();
bg_renderer->getBitmap()->writeImgFile(splashFormatPng, (char*)((param->single_html ? tmp_dir : dest_dir) / fn) .c_str(), param->h_dpi2, param->v_dpi2);
if(param->single_html)
add_tmp_file(fn);
}
doc->displayPage(this, i, param->h_dpi, param->v_dpi,
@ -86,11 +92,19 @@ void HTMLRenderer::process(PDFDoc *doc)
void HTMLRenderer::pre_process()
{
// we may output utf8 characters, so use binary
html_fout.open(working_dir() / param->output_filename, ofstream::binary);
allcss_fout.open(working_dir() / "all.css", ofstream::binary);
if(!param->single_html)
if(param->single_html)
{
html_fout.open(tmp_dir / param->output_filename, ofstream::binary);
allcss_fout.open(tmp_dir / "all.css", ofstream::binary);
add_tmp_file(param->output_filename);
add_tmp_file("all.css");
}
else
{
html_fout.open(dest_dir / param->output_filename, ofstream::binary);
allcss_fout.open(dest_dir / "all.css", ofstream::binary);
html_fout << ifstream(PDF2HTMLEX_LIB_PATH / "head.html", ifstream::binary).rdbuf();
html_fout << "<link rel=\"stylesheet\" type=\"text/css\" href=\"all.css\"/>" << endl;
html_fout << ifstream(PDF2HTMLEX_LIB_PATH / "neck.html", ifstream::binary).rdbuf();
@ -178,8 +192,31 @@ void HTMLRenderer::process_single_html()
out << ifstream(PDF2HTMLEX_LIB_PATH / "tail.html", ifstream::binary).rdbuf();
}
void HTMLRenderer::add_tmp_file(const string & fn)
{
if(tmp_files.insert(fn).second && param->debug)
cerr << "Add new temporary file: " << fn << endl;
}
void HTMLRenderer::clean_tmp_files()
{
for(const auto & fn : tmp_files)
{
try
{
remove(tmp_dir / fn);
if(param->debug)
cerr << "Remove temporary file: " << fn << endl;
}
catch(const filesystem_error &)
{ }
}
try
{
remove(tmp_dir);
if(param->debug)
cerr << "Remove temporary directory: " << tmp_dir << endl;
}
catch(const filesystem_error &)
{ }
}

View File

@ -121,6 +121,7 @@ void HTMLRenderer::install_embedded_font(GfxFont * font, const string & suffix,
path script_path = tmp_dir / "pdf2htmlEX.pe";
ofstream script_fout(script_path, ofstream::binary);
add_tmp_file("pdf2htmlEX.pe");
script_fout << format("Open(%1%, 1)") % (tmp_dir / (fn + suffix)) << endl;
@ -154,6 +155,8 @@ void HTMLRenderer::install_embedded_font(GfxFont * font, const string & suffix,
if(maxcode > 0)
{
ofstream map_fout(tmp_dir / (fn + ".encoding"));
add_tmp_file(fn+".encoding");
int cnt = 0;
for(int i = 0; i <= maxcode; ++i)
{
@ -180,9 +183,13 @@ void HTMLRenderer::install_embedded_font(GfxFont * font, const string & suffix,
ctu->decRefCnt();
}
script_fout << format("Generate(%1%)") % (working_dir() / (fn+".ttf")) << endl;
script_fout << format("Generate(%1%)") % ((param->single_html ? tmp_dir : dest_dir) / (fn+".ttf")) << endl;
if(param->single_html)
add_tmp_file(fn+".ttf");
system((boost::format("fontforge -script %1% 2>%2%") % script_path % (tmp_dir / "log.txt")).str().c_str());
// for cross-platform purpose, use a file instead of /dev/null
system((boost::format("fontforge -script %1% 2>%2%") % script_path % (tmp_dir / "null")).str().c_str());
add_tmp_file("null");
export_remote_font(fn_id, ".ttf", "truetype", font);
}

View File

@ -27,6 +27,7 @@ string HTMLRenderer::dump_embedded_font (GfxFont * font, long long fn_id)
char buf[1024];
int len;
string fn;
ofstream outf;
auto * id = font->getID();
@ -113,7 +114,10 @@ string HTMLRenderer::dump_embedded_font (GfxFont * font, long long fn_id)
}
obj.streamReset();
outf.open(tmp_dir / (format("f%|1$x|%2%")%fn_id%suffix).str(), ofstream::binary);
fn = (format("f%|1$x|%2%")%fn_id%suffix).str();
outf.open(tmp_dir / fn , ofstream::binary);
add_tmp_file(fn);
while((len = obj.streamGetChars(1024, (Guchar*)buf)) > 0)
{
outf.write(buf, len);

View File

@ -13,7 +13,6 @@
#include <algorithm>
#include <istream>
#include <ostream>
#include <iomanip>
#include <GfxState.h>
#include <CharTypes.h>
@ -23,8 +22,9 @@
using std::istream;
using std::ostream;
using std::endl;
using std::noskipws;
using std::endl;
using std::flush;
// mute gcc warning of unused function
namespace