Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Commit

Permalink
Render page as a base64 string.
Browse files Browse the repository at this point in the history
Provided API for the Webpage object:
- renderBase64PNG()
- renderBase64JPG()
- renderBase64BMP()

Addresses [Issue #547](http://code.google.com/p/phantomjs/issues/detail?id=547).
  • Loading branch information
detro authored and ariya committed Jun 15, 2012
1 parent ebc1855 commit 51ab11c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/webpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include <QWebPage>
#include <QWebInspector>
#include <QMapIterator>
#include <QBuffer>
#include <QDebug>

#include "networkaccessmanager.h"
Expand Down Expand Up @@ -546,6 +547,36 @@ bool WebPage::render(const QString &fileName)
return buffer.save(fileName);
}

QString WebPage::renderBase64PNG()
{
return renderBase64("PNG");
}

QString WebPage::renderBase64JPG()
{
return renderBase64("JPG");
}

QString WebPage::renderBase64BMP()
{
return renderBase64("BMP");
}

QString WebPage::renderBase64(const char *format)
{
QImage rawPageRendering = renderImage();

// Prepare buffer for writing
QByteArray bytes;
QBuffer buffer(&bytes);
buffer.open(QIODevice::WriteOnly);

// Writing image to the buffer, using PNG encoding
rawPageRendering.save(&buffer, format);

return bytes.toBase64();
}

QImage WebPage::renderImage()
{
QSize contentsSize = m_mainFrame->contentsSize();
Expand Down Expand Up @@ -593,7 +624,6 @@ QImage WebPage::renderImage()
}

m_webPage->setViewportSize(viewportSize);

return buffer;
}

Expand Down
4 changes: 4 additions & 0 deletions src/webpage.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ public slots:

QVariant evaluateJavaScript(const QString &code);
bool render(const QString &fileName);
QString renderBase64PNG();
QString renderBase64JPG();
QString renderBase64BMP();
bool injectJs(const QString &jsFilePath);
void _appendScriptElement(const QString &scriptUrl);
QObject *_getGenericCallback();
Expand All @@ -132,6 +135,7 @@ private slots:

private:
QImage renderImage();
QString renderBase64(const char *format = "PNG");
bool renderPdf(const QString &fileName);
void applySettings(const QVariantMap &defaultSettings);
QString userAgent() const;
Expand Down

0 comments on commit 51ab11c

Please sign in to comment.