1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-07-05 01:28:39 +00:00

Merge pull request #98 from rmorlok/master

Added ability to customize page name format for split rendering
This commit is contained in:
Lu Wang 2013-03-17 23:48:35 -07:00
commit eb45e472a7
9 changed files with 383 additions and 6 deletions

View File

@ -65,12 +65,34 @@ You need to modify the manifest if you do not want outline embedded.
.TP
.B --split-pages <0|1> (Default: 0)
If turned on, pages will be stored into separated files named as <output-filename>0.page, <output-filename>1.page, ...
If turned on, the pages, css, and outline will be stored into separated files and no consolidated <output-filename>.html will be generated.
Also the css and outline will be stored into separated files, and the will be no <output-filename>.html generated.
<output-filename> may be used to specify the format for the filenames for individual pages. <output-filename> may contain a %d placeholder to indicate where the page number should be placed. The placeholder supports a limited subset of normal numerical placeholders, including specified width and zero padding.
If <output-filename> does not contain a placeholder for the page number, the page number will be inserted directly before the file extension. If the filename does not have an extension, the page number will be placed at the end of the file name.
If <output-filename> is not specified, <input-filename> will be used for the output filename, replacing the extension with .page and adding the page number directly before the extension.
This switch is useful if you want pages to be loaded separately & dynamically -- in which case you need to compose the page yourself, and a supporting backend might be necessary.
.B Examples
.B pdf2htmlEX --split-pages 1 foo.pdf
Yields page files foo1.page, foo2.page, etc.
.B pdf2htmlEX --split-pages 1 foo.pdf bar.baz
Yields page files bar1.baz, bar2.baz, etc.
.B pdf2htmlEX --split-pages 1 foo.pdf page%dbar.baz
Yields page files page1bar.baz, page2bar.baz, etc.
.B pdf2htmlEX --split-pages 1 foo.pdf bar%03d.baz
Yields page files bar001.baz, bar002.baz, etc.
.TP
.B --dest-dir <dir> (Default: .)
Specify destination folder
@ -83,7 +105,7 @@ If it's empty, the file name will be determined automatically.
.TP
.B --outline-filename <filename> (Default: <none>)
Specify the filename of the generated outline file, if not embedded.
Specify the filename of the generated outline file, if not embedded.
If it's empty, the file name will be determined automatically.

View File

@ -101,7 +101,8 @@ void HTMLRenderer::process(PDFDoc *doc)
if(param->split_pages)
{
auto page_fn = str_fmt("%s/%s%d.page", param->dest_dir.c_str(), param->output_filename.c_str(), i);
auto filled_template_filename = str_fmt(param->output_filename.c_str(), i);
auto page_fn = str_fmt("%s/%s", param->dest_dir.c_str(), string((char*)filled_template_filename).c_str());
f_pages.fs.open((char*)page_fn, ofstream::binary);
if(!f_pages.fs)
throw string("Cannot open ") + (char*)page_fn + " for writing";

View File

@ -215,19 +215,40 @@ int main(int argc, char **argv)
if(get_suffix(param.input_filename) == ".pdf")
{
if(param.split_pages)
param.output_filename = s.substr(0, s.size() - 4);
{
param.output_filename = s.substr(0, s.size() - 4) + "%d.page";
sanitize_filename(param.output_filename);
}
else
{
param.output_filename = s.substr(0, s.size() - 4) + ".html";
}
}
else
{
if(param.split_pages)
param.output_filename = s;
{
param.output_filename = s + "%d.page";
sanitize_filename(param.output_filename);
}
else
{
param.output_filename = s + ".html";
}
}
}
else if(param.split_pages)
{
// Need to make sure we have a page number placeholder in the filename
if(!sanitize_filename(param.output_filename))
{
// Inject the placeholder just before the file extension
const string suffix = get_suffix(param.output_filename);
param.output_filename = param.output_filename.substr(0, param.output_filename.size() - suffix.size()) + "%d" + suffix;
sanitize_filename(param.output_filename);
}
}
if(param.css_filename.empty())
{

View File

@ -8,6 +8,7 @@
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <cstring>
#include "path.h"
@ -39,6 +40,69 @@ void create_directories(const string & path)
}
}
bool sanitize_filename(string & filename)
{
string sanitized;
bool format_specifier_found = false;
for(size_t i = 0; i < filename.size(); i++)
{
if('%' == filename[i])
{
if(format_specifier_found)
{
sanitized.push_back('%');
sanitized.push_back('%');
}
else
{
// We haven't found the format specifier yet, so see if we can use this one as a valid formatter
size_t original_i = i;
string tmp;
tmp.push_back('%');
while(++i < filename.size())
{
tmp.push_back(filename[i]);
// If we aren't still in option specifiers, stop looking
if(!strchr("0123456789", filename[i]))
{
break;
}
}
// Check to see if we yielded a valid format specifier
if('d' == tmp.back())
{
// Found a valid integer format
sanitized.append(tmp);
format_specifier_found = true;
}
else
{
// Not a valid format specifier. Just append the protected %
// and keep looking from where we left of in the search
sanitized.push_back('%');
sanitized.push_back('%');
i = original_i;
}
}
}
else
{
sanitized.push_back(filename[i]);
}
}
// Only sanitize if it is a valid format.
if(format_specifier_found)
{
filename.assign(sanitized);
}
return format_specifier_found;
}
bool is_truetype_suffix(const string & suffix)
{
return (suffix == ".ttf") || (suffix == ".ttc") || (suffix == ".otf");

View File

@ -19,5 +19,15 @@ bool is_truetype_suffix(const std::string & suffix);
std::string get_filename(const std::string & path);
std::string get_suffix(const std::string & path);
/**
* Sanitize all occurrences of '%' except for the first valid format specifier. Filename
* is only sanitized if a formatter is found, and the function returns true.
*
* @param filename the filename to be sanitized. Value will be modified.
*
* @return true if a format specifier was found, false otherwise.
*/
bool sanitize_filename(std::string & filename);
} //namespace pdf2htmlEX
#endif //PATH_H__

BIN
test/test_data/1-page.pdf Normal file

Binary file not shown.

BIN
test/test_data/2-pages.pdf Normal file

Binary file not shown.

BIN
test/test_data/3-pages.pdf Normal file

Binary file not shown.

259
test/test_naming.py Normal file
View File

@ -0,0 +1,259 @@
#!/usr/bin/env python
import unittest
import os
import sys
import tempfile
import shutil
import subprocess
# The location where the executable is generated by the build
PDF2HTMLEX_PATH = '../pdf2htmlEX'
# The location where the base css file, etc is stored in the build folder
DATA_DIR = '../share'
# The location where our test PDFs are stored
TEST_DATA_DIR = './test_data'
def execute_pdf2htmlex_with_args(args):
"""
Execute the pdf2htmlEX with the specified arguments.
:type args: list of values
:param args: list of arguments to pass to executable. First part of each tuple is the argument, second part is the value.
:rtype: int
:return: The exit code of the command
"""
executable = os.path.abspath(os.path.join(os.path.dirname(__file__), PDF2HTMLEX_PATH))
cmd = [executable, '--data-dir', os.path.abspath(os.path.join(os.path.dirname(__file__), DATA_DIR))]
for val in args:
cmd.append(str(val))
return_code = subprocess.call(cmd)
if return_code != 0:
print >> sys.stderr, "Command return code %d: %s" % (return_code, ' '.join(cmd))
return return_code
def execute_pdf2htmlex_and_get_files(args):
"""
Execute the pdf2htmlEX with the specified arguments, and get the names of the output files. Will automatically create
a temporary directory for the output, pass that as the output dir to pdf2htmlEX, determine the files generated, and
clean up the temporary directory afterwards.
:type args: list of values
:param args: list of arguments to pass to executable. First part of each tuple is the argument, second part is the value.
:rtype: list of str
:return: List of the file names that were generated as output in alphabetical order. None if the command does not execute successfully.
"""
temp_dir = tempfile.mkdtemp()
try:
if execute_pdf2htmlex_with_args(['--dest-dir', temp_dir] + args) != 0:
return None
files = os.listdir(temp_dir)
files.sort()
return files
finally:
shutil.rmtree(path=temp_dir, ignore_errors=True)
def path_to_test_file(filename):
"""
Retrieve an absolute path to the specified test file.
:type filename:
:param filename: the name of the test file to get the path to
:rtype: str
:returns: the full path to the test file
"""
return os.path.abspath(os.path.join(os.path.dirname(__file__), TEST_DATA_DIR, filename))
class OutputNamingTests(unittest.TestCase):
def test_generate_single_html_default_name_single_page_pdf(self):
files = execute_pdf2htmlex_and_get_files([
path_to_test_file('1-page.pdf')
])
self.assertEquals(files, ['1-page.html'])
def test_generate_single_html_default_name_multiple_page_pdf(self):
files = execute_pdf2htmlex_and_get_files([
path_to_test_file('2-pages.pdf')
])
self.assertEquals(files, ['2-pages.html'])
def test_generate_single_html_specify_name_single_page_pdf(self):
files = execute_pdf2htmlex_and_get_files([
path_to_test_file('1-page.pdf'),
'foo.html'
])
self.assertEquals(files, ['foo.html'])
def test_generate_single_html_specify_name_multiple_page_pdf(self):
files = execute_pdf2htmlex_and_get_files([
path_to_test_file('2-pages.pdf'),
'foo.html'
])
self.assertEquals(files, ['foo.html'])
def test_generate_split_pages_default_name_single_page(self):
files = execute_pdf2htmlex_and_get_files([
'--split-pages', 1,
path_to_test_file('1-page.pdf')
])
self.assertEquals(files, sorted(['1-page.css', '1-page.outline', '1-page1.page']))
def test_generate_split_pages_default_name_multiple_pages(self):
files = execute_pdf2htmlex_and_get_files([
'--split-pages', 1,
path_to_test_file('3-pages.pdf')
])
self.assertEquals(files, sorted(['3-pages.css', '3-pages.outline', '3-pages1.page', '3-pages2.page', '3-pages3.page']))
def test_generate_split_pages_specify_name_single_page(self):
files = execute_pdf2htmlex_and_get_files([
'--split-pages', 1,
path_to_test_file('1-page.pdf'),
'foo.xyz'
])
self.assertEquals(files, sorted(['1-page.css', '1-page.outline', 'foo1.xyz']))
def test_generate_split_pages_specify_name_multiple_pages(self):
files = execute_pdf2htmlex_and_get_files([
'--split-pages', 1,
path_to_test_file('3-pages.pdf'),
'foo.xyz'
])
self.assertEquals(files, sorted(['3-pages.css', '3-pages.outline', 'foo1.xyz', 'foo2.xyz', 'foo3.xyz']))
def test_generate_split_pages_specify_name_formatter_multiple_pages(self):
files = execute_pdf2htmlex_and_get_files([
'--split-pages', 1,
path_to_test_file('3-pages.pdf'),
'fo%do.xyz'
])
self.assertEquals(files, sorted(['3-pages.css', '3-pages.outline', 'fo1o.xyz', 'fo2o.xyz', 'fo3o.xyz']))
def test_generate_split_pages_specify_name_formatter_with_padded_zeros_multiple_pages(self):
files = execute_pdf2htmlex_and_get_files([
'--split-pages', 1,
path_to_test_file('3-pages.pdf'),
'fo%03do.xyz'
])
self.assertEquals(files, sorted(['3-pages.css', '3-pages.outline', 'fo001o.xyz', 'fo002o.xyz', 'fo003o.xyz']))
def test_generate_split_pages_specify_name_only_first_formatter_gets_taken(self):
files = execute_pdf2htmlex_and_get_files([
'--split-pages', 1,
path_to_test_file('3-pages.pdf'),
'f%do%do.xyz'
])
self.assertEquals(files, sorted(['3-pages.css', '3-pages.outline', 'f1o%do.xyz', 'f2o%do.xyz', 'f3o%do.xyz']))
def test_generate_split_pages_specify_name_only_percent_d_is_used_percent_s(self):
files = execute_pdf2htmlex_and_get_files([
'--split-pages', 1,
path_to_test_file('3-pages.pdf'),
'f%soo.xyz'
])
self.assertEquals(files, sorted(['3-pages.css', '3-pages.outline', 'f%soo1.xyz', 'f%soo2.xyz', 'f%soo3.xyz']))
def test_generate_split_pages_specify_name_only_percent_d_is_used_percent_p(self):
files = execute_pdf2htmlex_and_get_files([
'--split-pages', 1,
path_to_test_file('3-pages.pdf'),
'f%poo.xyz'
])
self.assertEquals(files, sorted(['3-pages.css', '3-pages.outline', 'f%poo1.xyz', 'f%poo2.xyz', 'f%poo3.xyz']))
def test_generate_split_pages_specify_name_only_percent_d_is_used_percent_n(self):
files = execute_pdf2htmlex_and_get_files([
'--split-pages', 1,
path_to_test_file('3-pages.pdf'),
'f%noo.xyz'
])
self.assertEquals(files, sorted(['3-pages.css', '3-pages.outline', 'f%noo1.xyz', 'f%noo2.xyz', 'f%noo3.xyz']))
def test_generate_split_pages_specify_name_only_percent_d_is_used_percent_percent(self):
files = execute_pdf2htmlex_and_get_files([
'--split-pages', 1,
path_to_test_file('3-pages.pdf'),
'f%%oo.xyz'
])
self.assertEquals(files, sorted(['3-pages.css', '3-pages.outline', 'f%%oo1.xyz', 'f%%oo2.xyz', 'f%%oo3.xyz']))
def test_generate_split_pages_specify_name_only_percent_d_is_used_percent_percent_with_actual_placeholder(self):
files = execute_pdf2htmlex_and_get_files([
'--split-pages', 1,
path_to_test_file('3-pages.pdf'),
'f%%o%do.xyz'
])
self.assertEquals(files, sorted(['3-pages.css', '3-pages.outline', 'f%%o1o.xyz', 'f%%o2o.xyz', 'f%%o3o.xyz']))
def test_generate_split_pages_specify_name_only_percent_d_is_used_percent_percent_with_actual_placeholder(self):
files = execute_pdf2htmlex_and_get_files([
'--split-pages', 1,
path_to_test_file('3-pages.pdf'),
'fo%do%%.xyz'
])
self.assertEquals(files, sorted(['3-pages.css', '3-pages.outline', 'fo1o%%.xyz', 'fo2o%%.xyz', 'fo3o%%.xyz']))
def test_generate_split_pages_specify_name_only_formatter_starts_part_way_through_invalid_formatter(self):
files = execute_pdf2htmlex_and_get_files([
'--split-pages', 1,
path_to_test_file('3-pages.pdf'),
'f%02%doo.xyz'
])
self.assertEquals(files, sorted(['3-pages.css', '3-pages.outline', 'f%021oo.xyz', 'f%022oo.xyz', 'f%023oo.xyz']))
def test_generate_split_pages_specify_output_filename_no_formatter_no_extension(self):
files = execute_pdf2htmlex_and_get_files([
'--split-pages', 1,
path_to_test_file('1-page.pdf'),
'foo'
])
self.assertEquals(files, sorted(['1-page.css', '1-page.outline', 'foo1']))
def test_generate_single_html_name_specified_format_characters_percent_d(self):
files = execute_pdf2htmlex_and_get_files([
path_to_test_file('2-pages.pdf'),
'foo%d.html'
])
self.assertEquals(files, ['foo%d.html'])
def test_generate_single_html_name_specified_format_characters_percent_p(self):
files = execute_pdf2htmlex_and_get_files([
path_to_test_file('2-pages.pdf'),
'foo%p.html'
])
self.assertEquals(files, ['foo%p.html'])
def test_generate_single_html_name_specified_format_characters_percent_n(self):
files = execute_pdf2htmlex_and_get_files([
path_to_test_file('2-pages.pdf'),
'foo%n.html'
])
self.assertEquals(files, ['foo%n.html'])
def test_generate_single_html_name_specified_format_characters_percent_percent(self):
files = execute_pdf2htmlex_and_get_files([
path_to_test_file('2-pages.pdf'),
'foo%%.html'
])
self.assertEquals(files, ['foo%%.html'])
if __name__=="__main__":
if not os.path.isfile(PDF2HTMLEX_PATH) or not os.access(PDF2HTMLEX_PATH, os.X_OK):
print >> sys.stderr, "Cannot locate pdf2htmlEX executable. Make sure source was built before running this test."
exit(1)
suite = unittest.loader.TestLoader().loadTestsFromTestCase(OutputNamingTests)
unittest.TextTestRunner(verbosity=2).run(suite)