Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include "RenderProcessHandler.h"

#include "include/base/cef_logging.h"
#include "include/base/cef_callback.h"
#include "include/wrapper/cef_helpers.h"
#include "include/base/cef_bind.h"
#include "include/wrapper/cef_closure_task.h"

#include <thread>
Expand Down Expand Up @@ -100,7 +100,7 @@ void Client::CreateBrowsers(unsigned int browserCount)
while (m_pendingBrowsersCount > 0 && m_browsersCount <= constants::maxProcesses) {
--m_pendingBrowsersCount;
++m_browsersCount;
CefBrowserHost::CreateBrowser(m_windowInfo, this, "", m_browserSettings, NULL, NULL);
CefBrowserHost::CreateBrowser(m_windowInfo, this, "", m_browserSettings, nullptr, nullptr);
}
}

Expand Down Expand Up @@ -230,7 +230,7 @@ void Client::OnBeforeClose(CefRefPtr<CefBrowser> browser)
--m_browsersCount;

if (0 == m_browsersCount && m_stopAfterLastJob) {
CefPostDelayedTask(TID_UI, base::Bind(&Client::Stop, this), 50);
CefPostDelayedTask(TID_UI, base::BindOnce(&Client::Stop, this), 50);
} else {
CreateBrowsers();
}
Expand Down Expand Up @@ -265,7 +265,7 @@ void Client::OnLoadEnd(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame,
if (frame->IsMain()) {
if (httpStatusCode == 200 && m_delay > 0) {
DLOG(INFO) << "Client::OnLoadEnd - waiting for " << m_delay << "ms before generating PDF";
CefPostDelayedTask(TID_UI, base::Bind(&Client::Process, this, browser), m_delay);
CefPostDelayedTask(TID_UI, base::BindOnce(&Client::Process, this, browser), m_delay);
}
else
m_jobManager->Process(browser, httpStatusCode);
Expand Down
22 changes: 21 additions & 1 deletion src/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
#include <random>
#include <sstream>
#include <iomanip>
#include <filesystem>

#if !defined(OS_WIN)
#include <sys/types.h> // pid_t
#include <sys/stat.h> // stat()
#include <unistd.h> // access(), getcwd()
#else
#include "Shlwapi.h" // PathFileExistsW
#include <direct.h>
#include <shlobj.h>
#include <Shlwapi.h> // PathFileExistsW
#endif

namespace cefpdf {
Expand Down Expand Up @@ -286,6 +289,23 @@ std::string pathToUri(const std::string& path)
return std::string("file://") + uri;
}

std::string getDownloadDirectory() {
TCHAR szFolderPath[MAX_PATH];
std::string path;

#if defined(OS_WIN)
// Save the file in the user's "My Documents" folder.
if (SUCCEEDED(SHGetFolderPath(nullptr, CSIDL_PERSONAL | CSIDL_FLAG_CREATE,
nullptr, 0, szFolderPath))) {
path = CefString(szFolderPath);
}
#else
path = getenv("HOME");
#endif // OS_WIN

return path;
}

std::string getCurrentWorkingDirectory()
{
#if defined(OS_WIN)
Expand Down
4 changes: 4 additions & 0 deletions src/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace cefpdf {

std::string getDownloadDirectory();

std::string getCurrentWorkingDirectory();

std::string getTempDirectory();
Expand All @@ -34,6 +36,8 @@ namespace constants {

// Current working directory
const std::string cwd = getCurrentWorkingDirectory();
// File save directory (including trailing slash)
const std::string save = getDownloadDirectory();
// Temp directory (including trailing slash)
const std::string tmp = getTempDirectory();
// Current process ID
Expand Down
58 changes: 58 additions & 0 deletions src/Job/Job.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "Job.h"

#include <podofo.h>

#include <filesystem>

namespace cefpdf {
namespace job {

Expand Down Expand Up @@ -43,6 +47,60 @@ void Job::SetScale(int scale)
m_scale = scale;
}

void Job::SetCover(const std::string& path)
{
m_cover = path;
}

void Job::SetAppendix(const std::string& path)
{
m_appendix = path;
}

void Job::SetStatus(Status status) {
m_status = status;

if (Status::SUCCESS == status) {
std::error_code code;
std::string orgFile = m_outputPath.ToString() + ".org.pdf";
if (!m_cover.empty() || !m_appendix.empty()) {
std::filesystem::rename(m_outputPath.ToString(), orgFile, code);
}
if (0 == code.value()) {
try {
if (!m_cover.empty() && m_appendix.empty()) {
PoDoFo::PdfMemDocument cover( m_cover.c_str() );
PoDoFo::PdfMemDocument save( orgFile.c_str() );

cover.Append( save );

cover.Write( m_outputPath.c_str() );
} else if (!m_cover.empty() && !m_appendix.empty()) {
PoDoFo::PdfMemDocument cover( m_cover.c_str() );
PoDoFo::PdfMemDocument save( orgFile.c_str() );
PoDoFo::PdfMemDocument appendix( m_appendix.c_str() );

cover.Append( save );
cover.Append( appendix );

cover.Write( m_outputPath.c_str() );
} else if (m_cover.empty() && !m_appendix.empty()) {
PoDoFo::PdfMemDocument save( orgFile.c_str() );
PoDoFo::PdfMemDocument appendix( m_appendix.c_str() );

save.Append( appendix );

save.Write( m_outputPath.c_str() );
}

std::filesystem::remove(orgFile);
} catch( PoDoFo::PdfError & e ) {
throw e;
}
}
}
}

CefPdfPrintSettings Job::GetCefPdfPrintSettings() const
{
CefPdfPrintSettings pdfSettings;
Expand Down
10 changes: 7 additions & 3 deletions src/Job/Job.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,18 @@ class Job : public CefBaseRefCounted

void SetScale(int scale);

void SetCover(const std::string& path);

void SetAppendix(const std::string& path);

// Get prepared PDF setting for CEF
CefPdfPrintSettings GetCefPdfPrintSettings() const;

Status GetStatus() {
return m_status;
}

void SetStatus(Status status) {
m_status = status;
}
void SetStatus(Status status);

private:
CefString m_outputPath;
Expand All @@ -81,6 +83,8 @@ class Job : public CefBaseRefCounted
Status m_status;
Callback m_callback;
int m_scale;
std::string m_cover;
std::string m_appendix;

// Include the default reference counting implementation.
IMPLEMENT_REFCOUNTING(Job);
Expand Down
8 changes: 4 additions & 4 deletions src/Job/Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void Manager::Assign(CefRefPtr<CefBrowser> browser)

m_jobsQueue.pop();

m_jobs.push_back(BrowserJob({browser, job, NULL}));
m_jobs.push_back(BrowserJob({browser, job, nullptr}));

job->SetStatus(Job::Status::LOADING);

Expand All @@ -62,9 +62,9 @@ void Manager::Process(CefRefPtr<CefBrowser> browser, int httpStatusCode)
if (it != m_jobs.end()) {
if (!httpStatusCode || (200 <= httpStatusCode && 300 > httpStatusCode)) {
// Generate file name if empty
if (it->job->GetOutputPath().empty()) {
it->job->SetOutputPath(reserveTempFile());
}
// if (it->job->GetOutputPath().empty()) {
// it->job->SetOutputPath(reserveTempFile());
// }

it->job->SetStatus(Job::Status::PRINTING);

Expand Down
3 changes: 2 additions & 1 deletion src/PrintHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ PrintHandler::PrintHandler() {}

// CefPrintHandler methods:
// -------------------------------------------------------------------------
CefSize PrintHandler::GetPdfPaperSize(int device_units_per_inch)
CefSize PrintHandler::GetPdfPaperSize(CefRefPtr<CefBrowser> browser,
int device_units_per_inch)
{
DLOG(INFO)
<< "PrintHandler::GetPdfPaperSize"
Expand Down
3 changes: 2 additions & 1 deletion src/PrintHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class PrintHandler : public CefPrintHandler
PrintHandler();

// CefPrintHandler methods:
virtual CefSize GetPdfPaperSize(int device_units_per_inch) override;
virtual CefSize GetPdfPaperSize(CefRefPtr<CefBrowser> browser,
int device_units_per_inch) override;

virtual bool OnPrintDialog(
CefRefPtr<CefBrowser> browser,
Expand Down
2 changes: 1 addition & 1 deletion src/SchemeHandlerFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ CefRefPtr<CefResourceHandler> SchemeHandlerFactory::Create(
auto streamReader = m_jobsManager->GetStreamReader(browser);

if (!streamReader.get()) {
return NULL;
return nullptr;
}

return new CefStreamResourceHandler("text/html", streamReader);
Expand Down
2 changes: 2 additions & 0 deletions src/Server/Http.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace headers {
const std::string location = "Content-Location";
const std::string encoding = "Transfer-Encoding";
const std::string expect = "Expect";
const std::string download = "Download";
const std::string pageSize = "PDF-Page-Size";
const std::string pageMargin = "PDF-Page-Margin";
const std::string pdfOptions = "PDF-Options";
Expand All @@ -52,6 +53,7 @@ struct Request {
std::size_t length;
std::string encoding;
bool chunked;
bool download;
std::string expect;
std::string location;
std::string pageSize;
Expand Down
11 changes: 7 additions & 4 deletions src/Server/Server.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Server.h"

#include "include/base/cef_callback.h"
#include "include/wrapper/cef_helpers.h"
#include "include/base/cef_bind.h"
#include "include/wrapper/cef_closure_task.h"

#include <iostream>
Expand All @@ -14,15 +14,18 @@ namespace server {
Server::Server(
CefRefPtr<cefpdf::Client> client,
const std::string& address,
const std::string& port
const std::string& port,
const std::string& save,
const std::string& temp,
const bool& persistent
) :
m_client(client),
m_thread(),
m_ioService(),
m_signals(m_ioService),
m_acceptor(m_ioService),
m_socket(m_ioService),
m_sessionManager(new SessionManager),
m_sessionManager(new SessionManager(save, temp, persistent)),
m_counter(0)
{
m_signals.add(SIGINT);
Expand Down Expand Up @@ -71,7 +74,7 @@ void Server::Run()
Listen();
m_ioService.run();

CefPostTask(TID_UI, base::Bind(&cefpdf::Client::Stop, m_client));
CefPostTask(TID_UI, base::BindOnce(&cefpdf::Client::Stop, m_client));

DLOG(INFO) << "HTTP server thread finished";
}
Expand Down
2 changes: 1 addition & 1 deletion src/Server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Server : public CefBaseRefCounted
{

public:
Server(CefRefPtr<cefpdf::Client> client, std::string const&, std::string const&);
Server(CefRefPtr<cefpdf::Client> client, std::string const& host, std::string const& port, std::string const& save, std::string const& temp, bool const& persistent);

void Start();

Expand Down
Loading