@@ -111,66 +111,55 @@ bool SplashBackgroundRenderer::render_page(PDFDoc * doc, int pageno)
111111 (!(param.use_cropbox )),
112112 false , false ,
113113 nullptr , nullptr , &annot_cb, &process_annotation);
114+
115+ auto * bitmap = getBitmap ();
116+
117+ 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 ());
118+
119+ SplashImageFileFormat splashImageFileFormat;
120+ if (format == " png" )
121+ splashImageFileFormat = splashFormatPng;
122+ else if (format == " jpg" )
123+ splashImageFileFormat = splashFormatJpeg;
124+ else
125+ throw string (" Image format not supported: " ) + format;
126+
127+ SplashError e = bitmap->writeImgFile (splashImageFileFormat, (const char *)fn, param.actual_dpi , param.actual_dpi );
128+ if (e != splashOk)
129+ throw string (" Cannot write background image. SplashErrorCode: " ) + std::to_string (e);
130+
131+ if (param.embed_image )
132+ html_renderer->tmp_files .add ((const char *)fn);
133+
114134 return true ;
115135}
116136
117137void SplashBackgroundRenderer::embed_image (int pageno)
118138{
119- auto * bitmap = getBitmap ();
120- // dump the background image only when it is not empty
121- if (bitmap->getWidth () >= 0 && bitmap->getHeight () >= 0 )
139+ auto & f_page = *(html_renderer->f_curpage );
140+
141+ f_page << " <img class=\" " << CSS::FULL_BACKGROUND_IMAGE_CN
142+ << " \" alt=\"\" src=\" " ;
143+
144+ if (param.embed_image )
145+ {
146+ auto path = html_renderer->str_fmt (" %s/bg%x.%s" , param.tmp_dir .c_str (), pageno, format.c_str ());
147+ ifstream fin ((char *)path, ifstream::binary);
148+ if (!fin)
149+ throw string (" Cannot read background image " ) + (char *)path;
150+
151+ auto iter = FORMAT_MIME_TYPE_MAP.find (format);
152+ if (iter == FORMAT_MIME_TYPE_MAP.end ())
153+ throw string (" Image format not supported: " ) + format;
154+
155+ string mime_type = iter->second ;
156+ f_page << " data:" << mime_type << " ;base64," << Base64Stream (fin);
157+ }
158+ else
122159 {
123- {
124- 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 ());
125- if (param.embed_image )
126- html_renderer->tmp_files .add ((const char *)fn);
127-
128- SplashImageFileFormat splashImageFileFormat;
129- if (format == " png" )
130- splashImageFileFormat = splashFormatPng;
131- else if (format == " jpg" )
132- splashImageFileFormat = splashFormatJpeg;
133- else
134- throw string (" Image format not supported: " ) + format;
135-
136- SplashError e = bitmap->writeImgFile (splashImageFileFormat, (const char *)fn, param.actual_dpi , param.actual_dpi );
137- if (e != splashOk)
138- throw string (" Cannot write background image. SplashErrorCode: " ) + std::to_string (e);
139- }
140-
141- double h_scale = html_renderer->text_zoom_factor () * DEFAULT_DPI / param.actual_dpi ;
142- double v_scale = html_renderer->text_zoom_factor () * DEFAULT_DPI / param.actual_dpi ;
143-
144- auto & f_page = *(html_renderer->f_curpage );
145- auto & all_manager = html_renderer->all_manager ;
146-
147- f_page << " <img class=\" " << CSS::BACKGROUND_IMAGE_CN
148- << " " << CSS::LEFT_CN << all_manager.left .install (0 .0L )
149- << " " << CSS::BOTTOM_CN << all_manager.bottom .install (0 .0L )
150- << " " << CSS::WIDTH_CN << all_manager.width .install (h_scale * bitmap->getWidth ())
151- << " " << CSS::HEIGHT_CN << all_manager.height .install (v_scale * bitmap->getHeight ())
152- << " \" alt=\"\" src=\" " ;
153-
154- if (param.embed_image )
155- {
156- auto path = html_renderer->str_fmt (" %s/bg%x.%s" , param.tmp_dir .c_str (), pageno, format.c_str ());
157- ifstream fin ((char *)path, ifstream::binary);
158- if (!fin)
159- throw string (" Cannot read background image " ) + (char *)path;
160-
161- auto iter = FORMAT_MIME_TYPE_MAP.find (format);
162- if (iter == FORMAT_MIME_TYPE_MAP.end ())
163- throw string (" Image format not supported: " ) + format;
164-
165- string mime_type = iter->second ;
166- f_page << " data:" << mime_type << " ;base64," << Base64Stream (fin);
167- }
168- else
169- {
170- f_page << (char *)html_renderer->str_fmt (" bg%x.%s" , pageno, format.c_str ());
171- }
172- f_page << " \" />" ;
160+ f_page << (char *)html_renderer->str_fmt (" bg%x.%s" , pageno, format.c_str ());
173161 }
162+ f_page << " \" />" ;
174163}
175164
176165} // namespace pdf2htmlEX
0 commit comments