mirror of
https://github.com/pdf2htmlEX/pdf2htmlEX.git
synced 2024-12-22 13:00:08 +00:00
do not embed empty bg image
This commit is contained in:
parent
b32892b01b
commit
7534466d75
@ -4,16 +4,32 @@
|
||||
* Copyright (C) 2012 Lu Wang <coolwanglu@gmail.com>
|
||||
*/
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include <PDFDoc.h>
|
||||
|
||||
#include "Base64Stream.h"
|
||||
|
||||
#include "SplashBackgroundRenderer.h"
|
||||
|
||||
namespace pdf2htmlEX {
|
||||
|
||||
using std::string;
|
||||
using std::ifstream;
|
||||
|
||||
const SplashColor SplashBackgroundRenderer::white = {255,255,255};
|
||||
|
||||
/*
|
||||
* SplashOutputDev::startPage would paint the whole page with the background color
|
||||
* And thus have modified region set to the whole page area
|
||||
* We do not want that.
|
||||
*/
|
||||
void SplashBackgroundRenderer::startPage(int pageNum, GfxState *state, XRef *xrefA)
|
||||
{
|
||||
SplashOutputDev::startPage(pageNum, state, xrefA);
|
||||
clearModRegion();
|
||||
}
|
||||
|
||||
void SplashBackgroundRenderer::drawChar(GfxState *state, double x, double y,
|
||||
double dx, double dy,
|
||||
double originX, double originY,
|
||||
@ -40,17 +56,50 @@ static GBool annot_cb(Annot *, void *) {
|
||||
return false;
|
||||
};
|
||||
|
||||
void SplashBackgroundRenderer::render_page(PDFDoc * doc, int pageno, const string & filename)
|
||||
void SplashBackgroundRenderer::render_page(PDFDoc * doc, int pageno)
|
||||
{
|
||||
doc->displayPage(this, pageno, param.h_dpi, param.v_dpi,
|
||||
0,
|
||||
(!(param.use_cropbox)),
|
||||
false, false,
|
||||
nullptr, nullptr, &annot_cb, nullptr);
|
||||
}
|
||||
|
||||
void SplashBackgroundRenderer::embed_image(int pageno)
|
||||
{
|
||||
int xmin, xmax, ymin, ymax;
|
||||
getModRegion(&xmin, &ymin, &xmax, &ymax);
|
||||
|
||||
// dump the background image only when it is not empty
|
||||
if((xmin <= xmax) && (ymin <= ymax))
|
||||
{
|
||||
{
|
||||
auto fn = html_renderer->str_fmt("%s/bg%x.png", (param.embed_image ? param.tmp_dir : param.dest_dir).c_str(), pageno);
|
||||
if(param.embed_image)
|
||||
html_renderer->tmp_files.add((char*)fn);
|
||||
|
||||
getBitmap()->writeImgFile(splashFormatPng,
|
||||
(char*)filename.c_str(),
|
||||
(char*)fn,
|
||||
param.h_dpi, param.v_dpi);
|
||||
}
|
||||
|
||||
auto & f_page = *(html_renderer->f_curpage);
|
||||
f_page << "<img class=\"" << CSS::BACKGROUND_IMAGE_CN
|
||||
<< "\" alt=\"\" src=\"";
|
||||
if(param.embed_image)
|
||||
{
|
||||
auto path = html_renderer->str_fmt("%s/bg%x.png", param.tmp_dir.c_str(), pageno);
|
||||
ifstream fin((char*)path, ifstream::binary);
|
||||
if(!fin)
|
||||
throw string("Cannot read background image ") + (char*)path;
|
||||
f_page << "data:image/png;base64," << Base64Stream(fin);
|
||||
}
|
||||
else
|
||||
{
|
||||
f_page << (char*)html_renderer->str_fmt("bg%x.png", pageno);
|
||||
}
|
||||
f_page << "\"/>";
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace pdf2htmlEX
|
||||
|
@ -27,13 +27,15 @@ public:
|
||||
static const SplashColor white;
|
||||
|
||||
SplashBackgroundRenderer(HTMLRenderer * html_renderer, const Param & param)
|
||||
: SplashOutputDev(splashModeRGB8, 4, gFalse, (SplashColorPtr)&white, gTrue, gTrue)
|
||||
: SplashOutputDev(splashModeRGB8, 4, gFalse, (SplashColorPtr)(&white), gTrue, gTrue)
|
||||
, html_renderer(html_renderer)
|
||||
, param(param)
|
||||
{ }
|
||||
|
||||
virtual ~SplashBackgroundRenderer() { }
|
||||
|
||||
virtual void startPage(int pageNum, GfxState *state, XRef *xrefA);
|
||||
|
||||
virtual void drawChar(GfxState *state, double x, double y,
|
||||
double dx, double dy,
|
||||
double originX, double originY,
|
||||
@ -49,7 +51,8 @@ public:
|
||||
SplashOutputDev::fill(state);
|
||||
}
|
||||
|
||||
void render_page(PDFDoc * doc, int pageno, const std::string & filename);
|
||||
void render_page(PDFDoc * doc, int pageno);
|
||||
void embed_image(int pageno);
|
||||
|
||||
protected:
|
||||
HTMLRenderer * html_renderer;
|
||||
|
@ -28,6 +28,8 @@
|
||||
#include "StateManager.h"
|
||||
#include "HTMLTextPage.h"
|
||||
|
||||
#include "BackgroundRenderer/BackgroundRenderer_forward.h"
|
||||
|
||||
#include "util/const.h"
|
||||
#include "util/misc.h"
|
||||
|
||||
@ -307,11 +309,18 @@ protected:
|
||||
int * width_list;
|
||||
|
||||
Preprocessor preprocessor;
|
||||
|
||||
// manage temporary files
|
||||
TmpFiles tmp_files;
|
||||
|
||||
// for string formatting
|
||||
StringFormatter str_fmt;
|
||||
|
||||
// render background image
|
||||
friend BackgroundRenderer;
|
||||
BackgroundRenderer * bg_renderer;
|
||||
|
||||
|
||||
struct {
|
||||
std::ofstream fs;
|
||||
std::string path;
|
||||
|
@ -17,9 +17,10 @@
|
||||
#include "pdf2htmlEX-config.h"
|
||||
#include "HTMLRenderer.h"
|
||||
#include "HTMLTextLine.h"
|
||||
#include "BackgroundRenderer/BackgroundRenderer.h"
|
||||
#include "Base64Stream.h"
|
||||
|
||||
#include "BackgroundRenderer/BackgroundRenderer.h"
|
||||
|
||||
#include "util/namespace.h"
|
||||
#include "util/ffw.h"
|
||||
#include "util/math.h"
|
||||
@ -96,7 +97,7 @@ void HTMLRenderer::process(PDFDoc *doc)
|
||||
///////////////////
|
||||
// Process pages
|
||||
|
||||
BackgroundRenderer * bg_renderer = nullptr;
|
||||
bg_renderer = nullptr;
|
||||
if(param.process_nontext)
|
||||
{
|
||||
bg_renderer = new BackgroundRenderer(this, param);
|
||||
@ -122,11 +123,7 @@ void HTMLRenderer::process(PDFDoc *doc)
|
||||
|
||||
if(param.process_nontext)
|
||||
{
|
||||
auto fn = str_fmt("%s/bg%x.png", (param.embed_image ? param.tmp_dir : param.dest_dir).c_str(), i);
|
||||
if(param.embed_image)
|
||||
tmp_files.add((char*)fn);
|
||||
|
||||
bg_renderer->render_page(doc, i, (char*)fn);
|
||||
bg_renderer->render_page(doc, i);
|
||||
}
|
||||
|
||||
doc->displayPage(this, i,
|
||||
@ -155,7 +152,10 @@ void HTMLRenderer::process(PDFDoc *doc)
|
||||
post_process();
|
||||
|
||||
if(bg_renderer)
|
||||
{
|
||||
delete bg_renderer;
|
||||
bg_renderer = nullptr;
|
||||
}
|
||||
|
||||
cerr << endl;
|
||||
}
|
||||
@ -215,21 +215,7 @@ void HTMLRenderer::startPage(int pageNum, GfxState *state, XRef * xref)
|
||||
|
||||
if(param.process_nontext)
|
||||
{
|
||||
(*f_curpage) << "<img class=\"" << CSS::BACKGROUND_IMAGE_CN
|
||||
<< "\" alt=\"\" src=\"";
|
||||
if(param.embed_image)
|
||||
{
|
||||
auto path = str_fmt("%s/bg%x.png", param.tmp_dir.c_str(), pageNum);
|
||||
ifstream fin((char*)path, ifstream::binary);
|
||||
if(!fin)
|
||||
throw string("Cannot read background image ") + (char*)path;
|
||||
(*f_curpage) << "data:image/png;base64," << Base64Stream(fin);
|
||||
}
|
||||
else
|
||||
{
|
||||
(*f_curpage) << (char*)str_fmt("bg%x.png", pageNum);
|
||||
}
|
||||
(*f_curpage) << "\"/>";
|
||||
bg_renderer->embed_image(pageNum);
|
||||
}
|
||||
|
||||
reset_state();
|
||||
|
Loading…
Reference in New Issue
Block a user