Socket
Socket
Sign inDemoInstall

@shelf/aws-lambda-libreoffice

Package Overview
Dependencies
53
Maintainers
56
Versions
26
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @shelf/aws-lambda-libreoffice

92 MB LibreOffice to fit inside AWS Lambda compressed with Brotli


Version published
Maintainers
56
Created

Readme

Source

aws-lambda-libreoffice

Helper package to work with LibreOffice in AWS Lambda

Install

$ yarn add @shelf/aws-lambda-libreoffice

Usage (For version 4.x; based on a Lambda Docker Image)

This version requires Node 16.x or higher.

First, you need to create a Docker image for your Lambda function. See the example at libreoffice-lambda-base-image repo.

Given you have packaged your Lambda function as a Docker image, you can now use this package:

const {convertTo, canBeConvertedToPDF} = require('@shelf/aws-lambda-libreoffice');

module.exports.handler = async () => {
  // assuming there is a document.docx file inside /tmp dir
  // original file will be deleted afterwards

  if (!canBeConvertedToPDF('document.docx')) {
    return false;
  }

  return convertTo('document.docx', 'pdf'); // returns /tmp/document.pdf
};

Usage (For version 3.x; based on a Lambda Layer)

This version requires Node 12.x or higher.

NOTE: Since version 2.0.0 npm package no longer ships the 85 MB LibreOffice but relies upon libreoffice-lambda-layer instead. Follow the instructions on how to add a lambda layer in that repo.

const {convertTo, canBeConvertedToPDF} = require('@shelf/aws-lambda-libreoffice');

module.exports.handler = async () => {
  // assuming there is a document.docx file inside /tmp dir
  // original file will be deleted afterwards

  if (!canBeConvertedToPDF('document.docx')) {
    return false;
  }

  return convertTo('document.docx', 'pdf'); // returns /tmp/document.pdf
};

Or if you want more control:

const {unpack, defaultArgs} = require('@shelf/aws-lambda-libreoffice');

await unpack(); // default path /tmp/instdir/program/soffice.bin

execSync(
  `/tmp/instdir/program/soffice.bin ${defaultArgs.join(
    ' '
  )} --convert-to pdf file.docx --outdir /tmp`
);

Troubleshooting

  • Please allocate at least 1536 MB of RAM for your Lambda function.
  • It works only in Amazon Linux 2, so it won't work locally on Linux or macOS. However, you could run it in Docker using lambci/lambda:nodejs12.x image
  • If some file fails to be converted to PDF, try converting it to PDF on your computer first. This might be an issue with LibreOffice itself

See Also

Test

Beside unit tests that could be run via yarn test, there are integration tests. Smoke test that it works: ./test/test.sh.

Publish

$ git checkout master
$ yarn version
$ yarn publish
$ git push origin master --tags

License

MIT © Shelf

Keywords

FAQs

Last updated on 20 Jun 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc