Skip to content

Commit

Permalink
Feature request: Mixed page orientation wkhtmltopdf#1564
Browse files Browse the repository at this point in the history
Add a command line option "--custom-orientations" to specify custom
page
orientation for each URL.

Example is

"wkhtmltopdf.exe
--custom-orientations PLP page1.html   page2.html
page3.html
test_unit_03.pdf"

In this example, three URLs are supplied and "PLP"
indicates page
orientation for each URL (Portrait for 1st, Landscape for
2nd and
Portrait for third)
  • Loading branch information
shamitv committed Nov 15, 2015
1 parent caa039a commit 0330bd0
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
10 changes: 10 additions & 0 deletions include/wkhtmltox/pdfsettings.hh
Expand Up @@ -124,6 +124,10 @@ struct DLL_PUBLIC PdfGlobal {
//! The file where in to store the output
QString out;

//! custom page orientations
QString pageSpecificOrientations;


QString documentTitle;

bool useCompression;
Expand Down Expand Up @@ -170,6 +174,11 @@ struct DLL_PUBLIC PdfObject {

QString page;

//! To override global landscape or portrate orientation
bool override_orientation;
QPrinter::Orientation orientation;


//! Header related settings
HeaderFooter header;

Expand Down Expand Up @@ -202,6 +211,7 @@ struct DLL_PUBLIC PdfObject {

QString get(const char * name);
bool set(const char * name, const QString & value);

};

DLL_PUBLIC QPrinter::PageSize strToPageSize(const char * s, bool * ok=0);
Expand Down
8 changes: 7 additions & 1 deletion src/lib/pdfconverter.cc
Expand Up @@ -307,7 +307,10 @@ void PdfConverterPrivate::preprocessPage(PageObject & obj) {
obj.page->mainFrame()->setScrollBarPolicy(Qt::Horizontal,Qt::ScrollBarAlwaysOff);
}


if (obj.settings.override_orientation){
printer->setOrientation(obj.settings.orientation);
}

QWebPrinter wp(obj.page->mainFrame(), printer, *painter);
obj.pageCount = obj.settings.pagesCount? wp.pageCount(): 0;
pageCount += obj.pageCount;
Expand Down Expand Up @@ -863,6 +866,9 @@ void PdfConverterPrivate::spoolPage(int page) {
void PdfConverterPrivate::spoolTo(int page) {
int pc=settings.collate?1:settings.copies;
const settings::PdfObject & ps = objects[currentObject].settings;
if (ps.override_orientation){
printer->setOrientation(ps.orientation);
}
while (objectPage < page) {
for (int pc_=0; pc_ < pc; ++pc_)
spoolPage(objectPage);
Expand Down
6 changes: 6 additions & 0 deletions src/lib/pdfsettings.hh
Expand Up @@ -173,6 +173,12 @@ struct DLL_PUBLIC PdfObject {

QString page;


//! To override global landscape or portrate orientation
bool override_orientation;
QPrinter::Orientation orientation;


//! Header related settings
HeaderFooter header;

Expand Down
2 changes: 1 addition & 1 deletion src/pdf/pdfarguments.cc
Expand Up @@ -189,7 +189,7 @@ PdfCommandLineParser::PdfCommandLineParser(PdfGlobal & s, QList<PdfObject> & ps)

addarg("lowquality",'l',"Generates lower quality pdf/ps. Useful to shrink the result document space", new ConstSetter<QPrinter::PrinterMode>(s.resolution,QPrinter::ScreenResolution));
addarg("title", 0, "The title of the generated pdf file (The title of the first document is used if not specified)", new QStrSetter(s.documentTitle,"text"));

addarg("custom-orientations", NULL, "", new QStrSetter(s.pageSpecificOrientations, NULL,false));
addarg("read-args-from-stdin", 0, "Read command line arguments from stdin", new ConstSetter<bool>(readArgsFromStdin, true) );

extended(true);
Expand Down
41 changes: 41 additions & 0 deletions src/pdf/pdfcommandlineparser.cc
Expand Up @@ -143,6 +143,8 @@ void PdfCommandLineParser::readme(FILE * fd, bool html) const {
void PdfCommandLineParser::parseArguments(int argc, const char ** argv, bool fromStdin) {
bool defaultMode = false;
int arg=1;
QList <QPrinter::Orientation> pageOrientations;


PdfObject def;

Expand All @@ -154,6 +156,39 @@ void PdfCommandLineParser::parseArguments(int argc, const char ** argv, bool fro

if (readArgsFromStdin && !fromStdin) return;

//Parse page-specific print orientation
def.orientation = this->globalSettings.orientation;
if (this->globalSettings.pageSpecificOrientations != NULL){
fprintf(stdout, "Custom page orientations specified.\n\n");
QPrinter::Orientation orientation;
def.override_orientation = true;
for (int i = 0; i < this->globalSettings.pageSpecificOrientations.size(); i++){
QChar c = globalSettings.pageSpecificOrientations.at(i);
char orientParam = c.toUpper().toLatin1();
if (orientParam == 'P'){
orientation = QPrinter::Portrait;
}
else{
if (orientParam == 'L'){
orientation = QPrinter::Landscape;
}
else{
orientation = def.orientation;
}
}
pageOrientations.append(orientation);
}
if (pageOrientations.size() < pageSettings.size()){
fprintf(stderr, "Custom page orientations not specified for all pages.\n\n");
EXIT_FAILURE;
}
}
else{
def.override_orientation = false;
}



//Parse page options
while (arg < argc-1) {
pageSettings.push_back(def);
Expand Down Expand Up @@ -195,6 +230,12 @@ void PdfCommandLineParser::parseArguments(int argc, const char ** argv, bool fro
}
QByteArray a(argv[arg]);
ps.page = QString::fromLocal8Bit(a);
if (!def.override_orientation){
ps.orientation = def.orientation;
}
else{
ps.orientation = pageOrientations.takeFirst();
}
++arg;
}
for (;arg < argc;++arg) {
Expand Down

0 comments on commit 0330bd0

Please sign in to comment.