elwerene / libreoffice-convert

MIT License
242 stars 94 forks source link

Corrupted PDF #6

Closed qtapostate closed 5 years ago

qtapostate commented 5 years ago

Hey! I'm trying to use this library for an enterprise use case (can't name the company at this time). We are trying to convert DOCX files into PDF format so they can be uploaded to AWS. When converting, the command succeeds and writes the PDF, but the PDF is corrupted. I copied the code directly from your example to use in a Typescript module.

import * as libre from 'libreoffice-convert';
// ...

async docxToPdf(fileName: string): Promise<boolean> {
    try {
      await new Promise((resolve, reject) => {
        const docx = fs.readFileSync(`${fileName}.docx`);
        libre.convert(docx, 'pdf', undefined, (done) => {
          console.log(done);
          fs.writeFileSync(`${fileName}.pdf`, done);
          resolve();
        });
      });
      return true;
    } catch (e) {
      this.logger.error('Encountered error while writing or converting to PDF.');
      this.logger.error(e);
      console.log(e);
      return false;
    }
  }```
elwerene commented 5 years ago

I fixed the example. done is only the error object. I fixed this part of your code:

async docxToPdf(fileName: string): Promise<boolean> {
    try {
      await new Promise((resolve, reject) => {
        const docx = fs.readFileSync(`${fileName}.docx`);
        libre.convert(docx, 'pdf', undefined, (err, done) => {
          if (err) {
            console.log(err);
            return false;
          }
          fs.writeFileSync(`${fileName}.pdf`, done);
          resolve();
        });
      });
      return true;
    } catch (e) {
      this.logger.error('Encountered error while writing or converting to PDF.');
      this.logger.error(e);
      console.log(e);
      return false;
    }
  }