npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

doconv

v0.1.5

Published

Doconv API client (documents converter service in docker)

Downloads

18

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 like http://domain.tld:port where is Doconv container started

Returns: Doconv client object.



Doconv client

formats()

Returns: Formats list object


convert({options})

Options:

  • filefiles property value from the <input type="file"> element. (Required)
  • format – format of target file. One from formats identifiers list which returns format() method. (Default pdf);
  • download – once converted file will be recieved, opens save dialog in browser. Ignoring if hook property specified. (Default false)
  • hook – URL where is a server which can recieve a converted file(look for Node API below). (Default false)
  • context – any serializable object, will be send to hook URL along with file. Place here any data you need in hook to handle recieved file.(Default false)

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 file
  • download([name]) – call it to save file on device. Optionaly you may specify any name for downloading file.

markup({options})

Options:

  • body - content for document generation.
  • markup - type of the content. Currently support html and markdown. Default html
  • format – format of target file. One from formats identifiers list which returns format() method. (Default pdf);
  • download – once converted file will be recieved, opens save dialog in browser. Ignoring if hook property specified. (Default false)
  • name - basename of result file. Default Document
  • pageWidth - page width in millimeters. Default 210
  • pageHeight - page height in millimeters. Default 297
  • marginTop - top margin of the page in millimeters. Default 15
  • marginBottom - bottom margin of the page in millimeters. Default 15
  • marginRight - right margin of the page in millimeters. Default 15
  • marginLeft - left margin of the page in millimeters. Default 15
  • 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). (Default false)
  • context – any serializable object, will be send to hook URL along with file. Place here any data you need in hook to handle recieved file.(Default false)

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 file
  • download([name]) – call it to save file on device. Optionaly you may specify any name for downloading file.


API Node


Doconv hook handler

parseHook(<request>)

Params:

Returns: If request is posted from Doconv service, returns object:

  • error – returned if there was error during converting
  • context – object from client, if provided
  • meta – 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 calling save or read methods.
    • read() – returns file's body as a Buffer.
    • save( <dir>,[filename] | <path> ) – save file in the specified directory. Optionaly you may specifiy a different filename also. Or you can just specifiy full path to the new file.

Doconv client creator

doconv([api_url])

Params:

  • api_url - URL like http://domain.tld:port where is Doconv container started

Returns: Doconv client object.



Doconv client

formats()

Returns: Formats list object


convert({options})

Options:

  • filefiles property value from the <input type="file"> element. (Required)
  • format – format of target file. One from formats identifiers list which returns format() method. (Default pdf);
  • hook – URL where is a server which can recieve a converted file(look for Node API below). (Default false)
  • context – any serializable object, will be send to hook URL along with file. Place here any data you need in hook to handle recieved file.(Default false)

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 file
  • save( <dir>,[filename] | <path> ) – save file in the specified directory. Optionaly you may specifiy a different filename 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 support html and markdown. Default html
  • format – format of target file. One from formats identifiers list which returns format() method. (Default pdf);
  • name - basename of result file. Default Document
  • pageWidth - page width in millimeters. Default 210
  • pageHeight - page height in millimeters. Default 297
  • marginTop - top margin of the page in millimeters. Default 15
  • marginBottom - bottom margin of the page in millimeters. Default 15
  • marginRight - right margin of the page in millimeters. Default 15
  • marginLeft - left margin of the page in millimeters. Default 15
  • 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). (Default false)
  • hook – URL where is a server which can recieve a converted file(look for Node API below). (Default false)
  • context – any serializable object, will be send to hook URL along with file. Place here any data you need in hook to handle recieved file.(Default false)

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 file
  • save( <dir>,[filename] | <path> ) – save file in the specified directory. Optionaly you may specifiy a different filename 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", 
    },
    ...
]