Skip to content

Commit

Permalink
tmp image then move; remove debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
andiwand committed Jan 3, 2025
1 parent b2bfeb5 commit dc61272
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
3 changes: 0 additions & 3 deletions pdf2htmlEX/share/pdf2htmlEX.js.in
Original file line number Diff line number Diff line change
Expand Up @@ -343,14 +343,11 @@ Viewer.prototype = {
var image = images[i];
(function(image) {
image.addEventListener('error', function() {
console.debug('image load error, retry in 1s', image);
setTimeout(function() {
console.debug('retrying image load', image);
image.src = image.src;
}, 1000);
});
})(image);
console.debug('image reload', image);
image.src = image.src;
}
}
Expand Down
16 changes: 10 additions & 6 deletions pdf2htmlEX/src/BackgroundRenderer/CairoBackgroundRenderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ bool CairoBackgroundRenderer::render_page(PDFDoc * doc, int pageno)
if (doc->getPageRotate(pageno) == 90 || doc->getPageRotate(pageno) == 270)
std::swap(page_height, page_width);

string fn = (char*)html_renderer->str_fmt("%s/bg%x.svg", (param.embed_image ? param.tmp_dir : param.dest_dir).c_str(), pageno);
if(param.embed_image)
html_renderer->tmp_files.add(fn);

std::string tmp_fn = html_renderer->str_fmt("%s/tmp_bg%x.%s", (param.embed_image ? param.tmp_dir : param.dest_dir).c_str(), pageno, format.c_str());
std::string fn = html_renderer->str_fmt("%s/bg%x.%s", (param.embed_image ? param.tmp_dir : param.dest_dir).c_str(), pageno, format.c_str());

surface = cairo_svg_surface_create(fn.c_str(), page_width * param.actual_dpi / DEFAULT_DPI, page_height * param.actual_dpi / DEFAULT_DPI);
surface = cairo_svg_surface_create(tmp_fn.c_str(), page_width * param.actual_dpi / DEFAULT_DPI, page_height * param.actual_dpi / DEFAULT_DPI);
cairo_svg_surface_restrict_to_version(surface, CAIRO_SVG_VERSION_1_2);
cairo_surface_set_fallback_resolution(surface, param.actual_dpi, param.actual_dpi);

Expand Down Expand Up @@ -174,15 +174,14 @@ bool CairoBackgroundRenderer::render_page(PDFDoc * doc, int pageno)
{
int n = 0;
char c;
ifstream svgfile(fn);
ifstream svgfile(tmp_fn);
//count of '<' in the file should be an approximation of node count.
while(svgfile >> c)
{
if (c == '<')
++n;
if (n > param.svg_node_count_limit)
{
html_renderer->tmp_files.add(fn);
return false;
}
}
Expand All @@ -192,6 +191,11 @@ bool CairoBackgroundRenderer::render_page(PDFDoc * doc, int pageno)
for (auto id : bitmaps_in_current_page)
++bitmaps_ref_count[id];

std::rename(tmp_fn.c_str(), fn.c_str());

if(param.embed_image)
html_renderer->tmp_files.add(fn);

return true;
}

Expand Down
12 changes: 8 additions & 4 deletions pdf2htmlEX/src/BackgroundRenderer/SplashBackgroundRenderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ bool SplashBackgroundRenderer::render_page(PDFDoc * doc, int pageno)

auto * bitmap = getBitmap();

auto fn = html_renderer->str_fmt("%s/bg%x.%s", (param.embed_image ? param.tmp_dir : param.dest_dir).c_str(), pageno, format.c_str());
if(param.embed_image)
html_renderer->tmp_files.add((const char *)fn);
std::string tmp_fn = html_renderer->str_fmt("%s/tmp_bg%x.%s", (param.embed_image ? param.tmp_dir : param.dest_dir).c_str(), pageno, format.c_str());
std::string fn = html_renderer->str_fmt("%s/bg%x.%s", (param.embed_image ? param.tmp_dir : param.dest_dir).c_str(), pageno, format.c_str());

SplashImageFileFormat splashImageFileFormat;
if(format == "png")
Expand All @@ -126,10 +125,15 @@ bool SplashBackgroundRenderer::render_page(PDFDoc * doc, int pageno)
else
throw string("Image format not supported: ") + format;

SplashError e = bitmap->writeImgFile(splashImageFileFormat, (const char *)fn, param.actual_dpi, param.actual_dpi);
SplashError e = bitmap->writeImgFile(splashImageFileFormat, tmp_fn.c_str(), param.actual_dpi, param.actual_dpi);
if (e != splashOk)
throw string("Cannot write background image. SplashErrorCode: ") + std::to_string(e);

std::rename(tmp_fn.c_str(), fn.c_str());

if(param.embed_image)
html_renderer->tmp_files.add(fn);

return true;
}

Expand Down

0 comments on commit dc61272

Please sign in to comment.