doconv
v0.1.5
Published
Doconv API client (documents converter service in docker)
Downloads
4
Maintainers
Readme
Doconv
Doconv API client for browser and nodejs.
Doconv
Doconv is a document converting service with simple HTTP API packed in Docker image. This package is the simplest way to interact with its API.
Install
npm install doconv
Using in Browser
// May need full path 'doconv/dist/browser/doconv.esm.js'
import doconv from 'doconv';
// Use URL where is Doconv container started
const dc = doconv('http://localhost:3000');
// Load list of all possible formats
const formats = await dc.formats();
console.log("Supported formats:",formats);
// Somewhere on the page:
// <input type="file" id="myFile" />
// <button onclick="convertFile">Convert</button>
async function convertFile(){
// Send file to the service and waiting responce
const result = await dc.convert({
file: document.getElementById('myFile').files[0],
format: 'pdf'
});
// When converted file ready, let user to download it
result.download();
}
Also doconv is available on CDNs like https://unpkg/doconv
Using in Node
Client
const {doconv} = require('doconv');
// Use URL where is Doconv container started
const dc = doconv('http://localhost:3000');
// Send file to the service and waiting responce
const result = await dc.convert({
file: '/home/user/document.docx',
format: 'pdf'
});
// Save converted document on specified path
result.save('/home/user/converted.pdf');
Hook handler
const {hookParser} = require('doconv');
//Express-like middleware
async function(request,responce,next){
try{
const result = await hookParser(request);
if(result.error) throw new Error('Error during converting: ' + result.error);
console.log('Recieved file:', result.file.filename);
await result.file.save('/home/user/myfilestorage');
}catch(err){
console.log('Error:' + err.message)
}
responce.end();
}
API Browser
Most of methods are asynchronus, use it as a Promise.
Doconv client creator
doconv([api_url])
Params:
api_url
- URL likehttp://domain.tld:port
where is Doconv container started
Returns: Doconv client object.
Doconv client
formats()
Returns: Formats list object
convert({options})
Options:
file
– files property value from the<input type="file">
element. (Required)format
– format of target file. One from formats identifiers list which returnsformat()
method. (Defaultpdf
);download
– once converted file will be recieved, opens save dialog in browser. Ignoring ifhook
property specified. (Defaultfalse
)hook
– URL where is a server which can recieve a converted file(look for Node API below). (Defaultfalse
)context
– any serializable object, will be send tohook
URL along with file. Place here any data you need in hook to handle recieved file.(Defaultfalse
)
Returns: If hook
property speciefed, will be return text confirmation, that file will be sended to hook's URL. Otherwise result will be an object:
body
- converted file body content.filename
– name of file.mime
– mime type of filedownload([name])
– call it to save file on device. Optionaly you may specify anyname
for downloading file.
markup({options})
Options:
body
- content for document generation.markup
- type of the content. Currently supporthtml
andmarkdown
. Defaulthtml
format
– format of target file. One from formats identifiers list which returnsformat()
method. (Defaultpdf
);download
– once converted file will be recieved, opens save dialog in browser. Ignoring ifhook
property specified. (Defaultfalse
)name
- basename of result file. DefaultDocument
pageWidth
- page width in millimeters. Default210
pageHeight
- page height in millimeters. Default297
marginTop
- top margin of the page in millimeters. Default15
marginBottom
- bottom margin of the page in millimeters. Default15
marginRight
- right margin of the page in millimeters. Default15
marginLeft
- left margin of the page in millimeters. Default15
pageBreak
- string which will be replaced with page break in result document. Default<!--PAGEBREAK-->
hook
– URL where is a server which can recieve a converted file(look for Node API below). (Defaultfalse
)context
– any serializable object, will be send tohook
URL along with file. Place here any data you need in hook to handle recieved file.(Defaultfalse
)
Returns: If hook
property speciefed, will be return text confirmation, that file will be sended to hook's URL. Otherwise result will be an object:
body
- converted file body content.filename
– name of file.mime
– mime type of filedownload([name])
– call it to save file on device. Optionaly you may specify anyname
for downloading file.
API Node
Doconv hook handler
parseHook(<request>)
Params:
<request>
– Node http-request object.
Returns: If request is posted from Doconv service, returns object:
error
– returned if there was error during convertingcontext
– object from client, if providedmeta
– some metadata for the input and converted files.file
– converted file object:filename
– name of the converted file.mime
– mime type of the file.size
– size in bytes.tmp
– path to temp file. It will be removed after callingsave
orread
methods.read()
– returns file's body as a Buffer.save( <dir>,[filename] | <path> )
– save file in the specifieddirectory
. Optionaly you may specifiy a differentfilename
also. Or you can just specifiy full path to the new file.
Doconv client creator
doconv([api_url])
Params:
api_url
- URL likehttp://domain.tld:port
where is Doconv container started
Returns: Doconv client object.
Doconv client
formats()
Returns: Formats list object
convert({options})
Options:
file
– files property value from the<input type="file">
element. (Required)format
– format of target file. One from formats identifiers list which returnsformat()
method. (Defaultpdf
);hook
– URL where is a server which can recieve a converted file(look for Node API below). (Defaultfalse
)context
– any serializable object, will be send tohook
URL along with file. Place here any data you need in hook to handle recieved file.(Defaultfalse
)
Returns: If hook
property speciefed, will be return text confirmation, that file will be sended to hook's URL. Otherwise result will be an object:
body
- converted file body content as a Buffer.filename
– name of file.mime
– mime type of filesave( <dir>,[filename] | <path> )
– save file in the specifieddirectory
. Optionaly you may specifiy a differentfilename
also. Or you can just specifiy full path to the new file.
markup({options})
Options:
body
- content for document generation.markup
- type of the content. Currently supporthtml
andmarkdown
. Defaulthtml
format
– format of target file. One from formats identifiers list which returnsformat()
method. (Defaultpdf
);name
- basename of result file. DefaultDocument
pageWidth
- page width in millimeters. Default210
pageHeight
- page height in millimeters. Default297
marginTop
- top margin of the page in millimeters. Default15
marginBottom
- bottom margin of the page in millimeters. Default15
marginRight
- right margin of the page in millimeters. Default15
marginLeft
- left margin of the page in millimeters. Default15
pageBreak
- string which will be replaced with page break in result document. Default<!--PAGEBREAK-->
hook
– URL where is a server which can recieve a converted file(look for Node API below). (Defaultfalse
)hook
– URL where is a server which can recieve a converted file(look for Node API below). (Defaultfalse
)context
– any serializable object, will be send tohook
URL along with file. Place here any data you need in hook to handle recieved file.(Defaultfalse
)
Returns: If hook
property speciefed, will be return text confirmation, that file will be sended to hook's URL. Otherwise result will be an object:
body
- converted file body content as a Buffer.filename
– name of file.mime
– mime type of filesave( <dir>,[filename] | <path> )
– save file in the specifieddirectory
. Optionaly you may specifiy a differentfilename
also. Or you can just specifiy full path to the new file.
Formats list object
Looks like this:
[
...,
{
// Group of format
"doctype": "document",
// Format identifier
"format": "docx",
// Text description
"description": "Microsoft Office Open XML",
// Extension of the files of this format
"ext": "docx",
// Mime type for this format
"mime": "application/msword",
},
...
]