Skip to content

Commit

Permalink
corrected broken implementation of printDirect with buffer data for n…
Browse files Browse the repository at this point in the history
…ode v.4.x.x tojocky#95
  • Loading branch information
tojocky committed Oct 22, 2015
1 parent cdf79d8 commit 85872d9
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 42 deletions.
37 changes: 37 additions & 0 deletions examples/printPDFFileInBuffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Windows does not support PDF formats, but you can use imagemagick-native to achieve conversion from PDF to EMF.

var printer = require("../lib"),
fs = require('fs'),
path = require('path'),
filename = process.argv[2],
printername = process.argv[3];

if(process.platform == 'win32') {
throw 'Not yet supported for win32'
}

if(!filename || filename == '-h') {
throw 'PDF file name is missing. Please use the following params: <filename> [printername]'
}

filename = path.resolve(process.cwd(), filename);
console.log('printing file name ' + filename);

fs.readFile(filename, function(err, data){
if(err) {
console.error('err:' + err);
return;
}
console.log('data type is: '+typeof(data) + ', is buffer: ' + Buffer.isBuffer(data));
printer.printDirect({
data: data,
type: 'PDF',
success: function(id) {
console.log('printed with id ' + id);
},
error: function(err) {
console.error('error on printing: ' + err);
}
})
});

Binary file added examples/test.pdf
Binary file not shown.
2 changes: 2 additions & 0 deletions src/macros.hh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
# define RETURN_EXCEPTION_STR(msg) RETURN_EXCEPTION(V8_STRING_NEW_UTF8(msg))
# define MY_NODE_MODULE_RETURN_VALUE(value) iArgs.GetReturnValue().Set(value); \
return
# define MY_NODE_MODULE_RETURN_UNDEFINED() return
#else
# define MY_NODE_MODULE_ISOLATE_DECL
# define MY_NODE_MODULE_ISOLATE
Expand All @@ -40,6 +41,7 @@

# define RETURN_EXCEPTION_STR(msg) RETURN_EXCEPTION(V8_STRING_NEW_UTF8(msg))
# define MY_NODE_MODULE_RETURN_VALUE(value) return scope.Close(value)
# define MY_NODE_MODULE_RETURN_UNDEFINED() return scope.Close(v8::Undefined())
#endif

#if NODE_VERSION_AT_LEAST(0, 11, 10) // for node-webkit v.0.9.2 which uses node v0.11.9
Expand Down
21 changes: 20 additions & 1 deletion src/node_printer.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "node_printer.hpp"

#include <node_buffer.h>

void initNode(v8::Handle<v8::Object> exports) {
// only for node
NODE_SET_METHOD(exports, "getPrinters", getPrinters);
Expand All @@ -12,7 +14,24 @@ void initNode(v8::Handle<v8::Object> exports) {
NODE_SET_METHOD(exports, "printFile", PrintFile);
NODE_SET_METHOD(exports, "getSupportedPrintFormats", getSupportedPrintFormats);
NODE_SET_METHOD(exports, "getSupportedJobCommands", getSupportedJobCommands);

}

NODE_MODULE(node_printer, initNode);

// Helpers

bool getStringOrBufferFromV8Value(v8::Handle<v8::Value> iV8Value, std::string &oData)
{
if(iV8Value->IsString())
{
v8::String::Utf8Value data_str_v8(iV8Value->ToString());
oData.assign(*data_str_v8, data_str_v8.length());
return true;
}
if(iV8Value->IsObject() && node::Buffer::HasInstance(iV8Value))
{
oData.assign(node::Buffer::Data(iV8Value), node::Buffer::Length(iV8Value));
return true;
}
return false;
}
9 changes: 9 additions & 0 deletions src/node_printer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <node.h>
#include <v8.h>

#include <string>

/**
* Send data to printer
*
Expand Down Expand Up @@ -112,5 +114,12 @@ class MemValueBase
virtual void free() {};
};

/**
* try to extract String or buffer from v8 value
* @param iV8Value - source v8 value
* @param oData - destination data
* @return TRUE if value is String or Buffer, FALSE otherwise
*/
bool getStringOrBufferFromV8Value(v8::Handle<v8::Value> iV8Value, std::string &oData);

#endif
22 changes: 2 additions & 20 deletions src/node_printer_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ MY_NODE_MODULE_CALLBACK(getDefaultPrinterName)
MY_NODE_MODULE_RETURN_VALUE(V8_STRING_NEW_UTF8(printerName));
}
*/
MY_NODE_MODULE_RETURN_UNDEFINED();
}

MY_NODE_MODULE_CALLBACK(getPrinter)
Expand Down Expand Up @@ -452,26 +453,7 @@ MY_NODE_MODULE_CALLBACK(PrintDirect)

std::string data;
v8::Handle<v8::Value> arg0(iArgs[0]);

if(arg0->IsString())
{
v8::String::Utf8Value data_str_v8(arg0->ToString());
data.assign(*data_str_v8, data_str_v8.length());
}
#if NODE_VERSION_AT_LEAST(4,0,0)
else if(arg0->IsObject())
{
v8::ArrayBuffer::Contents data_contents = arg0.As<v8::ArrayBuffer>()->GetContents();
data.assign(static_cast<char*>(data_contents.Data()), data_contents.ByteLength());
}
#else
else if(arg0->IsObject() && arg0.As<v8::Object>()->HasIndexedPropertiesInExternalArrayData())
{
data.assign(static_cast<char*>(arg0.As<v8::Object>()->GetIndexedPropertiesExternalArrayData()),
arg0.As<v8::Object>()->GetIndexedPropertiesExternalArrayDataLength());
}
#endif
else
if (!getStringOrBufferFromV8Value(arg0, data))
{
RETURN_EXCEPTION_STR("Argument 0 must be a string or Buffer");
}
Expand Down
23 changes: 2 additions & 21 deletions src/node_printer_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -675,28 +675,9 @@ MY_NODE_MODULE_CALLBACK(PrintDirect)

std::string data;
v8::Handle<v8::Value> arg0(iArgs[0]);

if(arg0->IsString())
{
v8::String::Utf8Value data_str_v8(arg0->ToString());
data.assign(*data_str_v8, data_str_v8.length());
}
#if NODE_VERSION_AT_LEAST(4,0,0)
else if(arg0->IsObject())
{
v8::ArrayBuffer::Contents data_contents = arg0.As<v8::ArrayBuffer>()->GetContents();
data.assign(static_cast<char*>(data_contents.Data()), data_contents.ByteLength());
}
#else
else if(arg0->IsObject() && arg0.As<v8::Object>()->HasIndexedPropertiesInExternalArrayData())
{
data.assign(static_cast<char*>(arg0.As<v8::Object>()->GetIndexedPropertiesExternalArrayData()),
arg0.As<v8::Object>()->GetIndexedPropertiesExternalArrayDataLength());
}
#endif
else
if (!getStringOrBufferFromV8Value(arg0, data))
{
RETURN_EXCEPTION_STR("Argument 0 must be a string or NativeBuffer");
RETURN_EXCEPTION_STR("Argument 0 must be a string or Buffer");
}

REQUIRE_ARGUMENT_STRINGW(iArgs, 1, printername);
Expand Down

0 comments on commit 85872d9

Please sign in to comment.