//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------
// The following sample illustrates how to use the PDF::Convert utility class to convert 
// documents and files to PDF, XPS, or SVG, or EMF. The sample also shows how to convert MS Office files 
// using our built in conversion.
//
// Certain file formats such as XPS, EMF, PDF, and raster image formats can be directly 
// converted to PDF or XPS. 
//
// Please contact us if you have any questions.	
//---------------------------------------------------------------------------------------

const { PDFNet } = require('@pdftron/pdfnet-node');
const PDFTronLicense = require('../LicenseKey/LicenseKey');

((exports) => {
  'use strict';

  let Testfile = function (inputFile, outputFile) {
    this.inputFile = inputFile;
    this.outputFile = outputFile;
  }

  const testfiles = [
    new Testfile('simple-word_2007.docx', 'docx2pdf.pdf'),
    new Testfile('simple-powerpoint_2007.pptx', 'pptx2pdf.pdf'),
    new Testfile('simple-excel_2007.xlsx', 'xlsx2pdf.pdf'),
    new Testfile('simple-text.txt', 'txt2pdf.pdf'),
    new Testfile('butterfly.png', 'png2pdf.pdf'),
    new Testfile('simple-xps.xps', 'xps2pdf.pdf'),
  ]

  const inputPath = '../TestFiles/';
  const outputPath = '../TestFiles/Output/';

  exports.runConvertTest = () => {

    const main = async () => {
      try {
        await convertToPdfFromFile();
        console.log('ConvertFile succeeded');
      } catch (err) {
        console.log('ConvertFile failed');
        console.log(err);
      }

      try {
        await convertSpecificFormats();
        console.log('ConvertSpecificFormats succeeded');
      } catch (err) {
        console.log('ConvertSpecificFormats failed');
        console.log(err);
      }

      console.log('Done.');
    };

    const convertToPdfFromFile = async () => {

      for (const testfile of testfiles) {

        try {
          const pdfdoc = await PDFNet.PDFDoc.create();
          await pdfdoc.initSecurityHandler();
          const inputFile = inputPath + testfile.inputFile;
          const outputFile = outputPath + testfile.outputFile;
          await PDFNet.Convert.printerSetMode(PDFNet.Convert.PrinterMode.e_prefer_builtin_converter);
          await PDFNet.Convert.toPdf(pdfdoc, inputFile);
          await pdfdoc.save(outputFile, PDFNet.SDFDoc.SaveOptions.e_linearized);
          console.log('Converted file: ' + testfile.inputFile + '\nto: ' + testfile.outputFile);
        } catch (err) {
          console.log('Unable to convert file ' + testfile.inputFile);
          console.log(err);
        }
      }
    };

    const convertSpecificFormats = async () => {
      try {
        const pdfdoc = await PDFNet.PDFDoc.create();
        await pdfdoc.initSecurityHandler();

        console.log('Converting from XPS');
        await PDFNet.Convert.fromXps(pdfdoc, inputPath + 'simple-xps.xps');
        await pdfdoc.save(outputPath + 'xps2pdf v2.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
        console.log('Saved xps2pdf v2.pdf');
      } catch (err) {
        console.log(err);
      }


      try {
        const pdfdoc = await PDFNet.PDFDoc.create();
        await pdfdoc.initSecurityHandler();

        // Add a dictionary
        const set = await PDFNet.ObjSet.create();
        const options = await set.createDict();

        // Put options
        options.putNumber('FontSize', 15);
        options.putBool('UseSourceCodeFormatting', true);
        options.putNumber('PageWidth', 12);
        await options.putNumber('PageHeight', 6);

        // Convert from .txt file
        console.log('Converting from txt');
        await PDFNet.Convert.fromText(pdfdoc, inputPath + 'simple-text.txt', options);
        await pdfdoc.save(outputPath + 'simple-text.pdf', PDFNet.SDFDoc.SaveOptions.e_remove_unused);
        console.log('Saved simple-text.pdf');
      } catch (err) {
        console.log(err);
      }

      try {
        const pdfdoc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'newsletter.pdf');
        await pdfdoc.initSecurityHandler();

        // Convert PDF document to SVG
        console.log('Converting pdfdoc to SVG');
        await PDFNet.Convert.docToSvg(pdfdoc, outputPath + 'pdf2svg v2.svg');
        console.log('Saved pdf2svg v2.svg');
      } catch (err) {
        console.log(err);
      }

      try {
        // Convert PNG image to XPS
        console.log('Converting PNG to XPS');
        await PDFNet.Convert.fileToXps(inputPath + 'butterfly.png', outputPath + 'butterfly.xps');
        console.log('Saved butterfly.xps');
      } catch (err) {
        console.log(err);
      }


      try {
        // Convert PDF document to XPS
        console.log('Converting PDF to XPS');
        await PDFNet.Convert.fileToXps(inputPath + 'newsletter.pdf', outputPath + 'newsletter.xps');
        console.log('Saved newsletter.xps');
      } catch (err) {
        console.log(err);
      }

      try {
        // Convert PDF document to HTML
        console.log('Converting PDF to HTML');
        await PDFNet.Convert.fileToHtml(inputPath + 'newsletter.pdf', outputPath + 'newsletter');
        console.log('Saved newsletter as HTML');
      } catch (err) {
        console.log(err);
      }

      try {
        // Convert PDF document to EPUB
        console.log('Converting PDF to EPUB');
        await PDFNet.Convert.fileToEpub(inputPath + 'newsletter.pdf', outputPath + 'newsletter.epub');
        console.log('Saved newsletter.epub');
      } catch (err) {
        console.log(err);
      }

      try {
        // Convert PDF document to multipage TIFF
        console.log('Converting PDF to multipage TIFF');
        const tiff_options = new PDFNet.Convert.TiffOutputOptions();
        tiff_options.setDPI(200);
        tiff_options.setDither(true);
        tiff_options.setMono(true);
        
        await PDFNet.Convert.fileToTiff(inputPath + 'newsletter.pdf', outputPath + 'newsletter.tiff', tiff_options);
        console.log('Saved newsletter.tiff');
      } catch (err) {
        console.log(err);
      }

      try {
        const pdfdoc = await PDFNet.PDFDoc.create();
        await pdfdoc.initSecurityHandler();

        // Convert SVG file to PDF
        console.log('Converting SVG to PDF');
        await PDFNet.Convert.fromSVG(pdfdoc, inputPath + 'tiger.svg');
        await pdfdoc.save(outputPath + 'svg2pdf.pdf', PDFNet.SDFDoc.SaveOptions.e_remove_unused);

        console.log('Saved svg2pdf.pdf');
      } catch (err) {
        console.log(err);
      }
    };
    PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function(error) {
      console.log('Error: ' + JSON.stringify(error));
    }).then(function(){ return PDFNet.shutdown(); });
  };
  exports.runConvertTest();
})(exports);
// eslint-disable-next-line spaced-comment
//# sourceURL=ConvertTest.js
