forked from ariya/phantomjs
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c66f38
commit c718f83
Showing
7 changed files
with
180 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/*jslint sloppy: true, nomen: true */ | ||
/*global exports:true,phantom:true */ | ||
|
||
exports.create = function (opts) { | ||
return phantom.createPrinter(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
|
||
#include "printer.h" | ||
|
||
#include <math.h> | ||
|
||
#define PHANTOMJS_PDF_DPI 72 // Different defaults. OSX: 72, X11: 75(?), Windows: 96 | ||
|
||
Printer::Printer(QObject *parent) : | ||
QObject(parent), | ||
m_isFirstPage(true) | ||
{ | ||
m_printer = new QPrinter(); | ||
m_printer->setOutputFormat(QPrinter::PdfFormat); | ||
m_printer->setResolution(PHANTOMJS_PDF_DPI); | ||
|
||
m_painter = new QPainter(); | ||
} | ||
|
||
Printer::~Printer() | ||
{ | ||
delete m_painter; | ||
delete m_printer; | ||
} | ||
|
||
bool Printer::begin() | ||
{ | ||
return m_painter->begin(m_printer); | ||
} | ||
|
||
// Do not change the signature argument as this is introspected by the Qt type API to bind JavaScript calls | ||
void Printer::printPage(QObject * object) | ||
{ | ||
if (!m_isFirstPage) | ||
m_printer->newPage(); | ||
else | ||
m_isFirstPage = false; | ||
|
||
WebPage* page = (WebPage*) object; | ||
QWebFrame* frame = page->mainFrame(); | ||
frame->render(m_painter, QRegion(QRect(QPoint(0, 0), frame->contentsSize()))); | ||
} | ||
|
||
bool Printer::end() | ||
{ | ||
return m_painter->end(); | ||
} | ||
|
||
QString Printer::outputFileName() const | ||
{ | ||
return m_printer->outputFileName(); | ||
} | ||
|
||
void Printer::setOutputFileName(const QString &fileName) | ||
{ | ||
m_printer->setOutputFileName(fileName); | ||
} | ||
|
||
QVariantMap Printer::paperSize() const | ||
{ | ||
return m_paperSize; | ||
} | ||
|
||
void Printer::setPaperSize(const QVariantMap &paperSize) | ||
{ | ||
m_paperSize = paperSize; | ||
|
||
const QSizeF sizePt(ceil(stringToPointSize(paperSize.value("width").toString())), | ||
ceil(stringToPointSize(paperSize.value("height").toString()))); | ||
m_printer->setPaperSize(sizePt, QPrinter::Point); | ||
|
||
qreal marginLeft = 0, marginTop = 0, marginRight = 0, marginBottom = 0; | ||
if (paperSize.contains("margin")) { | ||
const QVariant margins = paperSize["margin"]; | ||
if (margins.canConvert(QVariant::Map)) { | ||
const QVariantMap map = margins.toMap(); | ||
marginLeft = printMargin(map, "left"); | ||
marginTop = printMargin(map, "top"); | ||
marginRight = printMargin(map, "right"); | ||
marginBottom = printMargin(map, "bottom"); | ||
} else if (margins.canConvert(QVariant::String)) { | ||
const qreal margin = stringToPointSize(margins.toString()); | ||
marginLeft = margin; | ||
marginTop = margin; | ||
marginRight = margin; | ||
marginBottom = margin; | ||
} | ||
} | ||
m_printer->setPageMargins(marginLeft, marginTop, marginRight, marginBottom, QPrinter::Point); | ||
} | ||
|
||
qreal Printer::stringToPointSize(const QString &string) | ||
{ | ||
static const struct { | ||
QString unit; | ||
qreal factor; | ||
} units[] = { | ||
{ "mm", 72 / 25.4 }, | ||
{ "cm", 72 / 2.54 }, | ||
{ "in", 72 }, | ||
{ "px", 72.0 / PHANTOMJS_PDF_DPI }, | ||
{ "", 72.0 / PHANTOMJS_PDF_DPI } | ||
}; | ||
for (uint i = 0; i < sizeof(units) / sizeof(units[0]); ++i) { | ||
if (string.endsWith(units[i].unit)) { | ||
QString value = string; | ||
value.chop(units[i].unit.length()); | ||
return value.toDouble() * units[i].factor; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
qreal Printer::printMargin(const QVariantMap &map, const QString &key) | ||
{ | ||
const QVariant margin = map.value(key); | ||
if (margin.isValid() && margin.canConvert(QVariant::String)) | ||
return stringToPointSize(margin.toString()); | ||
else | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
#ifndef PRINTER_H | ||
#define PRINTER_H | ||
|
||
#include <QPrinter> | ||
#include <QPainter> | ||
|
||
#include "webpage.h" | ||
|
||
class Printer : public QObject | ||
{ | ||
Q_OBJECT | ||
Q_PROPERTY(QString outputFileName READ outputFileName WRITE setOutputFileName) | ||
Q_PROPERTY(QVariantMap paperSize READ paperSize WRITE setPaperSize) | ||
|
||
public: | ||
Printer(QObject *parent); | ||
~Printer(); | ||
|
||
void setOutputFileName(const QString &); | ||
QString outputFileName() const; | ||
|
||
void setPaperSize(const QVariantMap &size); | ||
QVariantMap paperSize() const; | ||
|
||
public slots: | ||
bool begin(); | ||
void printPage(QObject *); | ||
bool end(); | ||
|
||
private: | ||
QPrinter *m_printer; | ||
QPainter *m_painter; | ||
QVariantMap m_paperSize; | ||
bool m_isFirstPage; | ||
|
||
qreal stringToPointSize(const QString &); | ||
qreal printMargin(const QVariantMap &, const QString &); | ||
}; | ||
|
||
#endif // PRINTER_H |