Some test text!

menu

JavaScript CAD to PDF converter (DWG, DXF, DGN, DWF, and RVT)

More languages

chevron_right
More languages
C++
C#
C# (.NET Core)
Java
JS (Node.js)
VB

Sample JavaScript code demonstrates how to use the PDFTron CAD module for direct, high-quality conversion from DWG, DXF, DGN, DWF, and RVT to PDF. Learn more about our JavaScript PDF Library and PDF Conversion Library.

Get StartedSamplesDownload

To run this sample, get started with a free trial of PDFTron SDK.

//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------


const { PDFNet } = require('../../../lib/pdfnet.js');

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

  exports.runCAD2PDFTest = () => {
    const IsRVTFile = function (inputFile) {
      var rvt_input = false;
      if (inputFile.length > 2) {
        if (inputFile.substr(inputFile.length - 3, 3) === "rvt") {
          rvt_input = true;
        }
      }
      return rvt_input;
    }

    const main = async () => {
      try {
        console.log('Beginning Test');
        // Relative path to the folder containing test files.
        const inputPath = '../../TestFiles/CAD/';
        const outputPath = '../../TestFiles/Output/';

        const input_file_name = "construction drawings color-28.05.18.dwg";
        const output_file_name = "construction drawings color-28.05.18.pdf";

        PDFNet.addResourceSearchPath("../../../lib/");
        if (!(await PDFNet.CADModule.isModuleAvailable())) {
          console.log("\nUnable to run CAD2PDFTest: PDFTron SDK CAD module not available.");
          console.log("---------------------------------------------------------------");
          console.log("The CAD module is an optional add-on, available for download");
          console.log("at http://www.pdftron.com/. If you have already downloaded this");
          console.log("module, ensure that the SDK is able to find the required files");
          console.log("using the PDFNet.addResourceSearchPath() function.\n");

          return;
        }

        console.log("Converting DWG CAD document to PDF");
        const doc = await PDFNet.PDFDoc.create();
        doc.initSecurityHandler();

        if (IsRVTFile(input_file_name)) {
          const opts = new PDFNet.Convert.CADConvertOptions();
					opts.setPageWidth(800);
					opts.setPageHeight(600);
					opts.setRasterDPI(150);
          await PDFNet.Convert.fromCAD(doc, inputPath + input_file_name, opts);
        } else {
          await PDFNet.Convert.fromCAD(doc, inputPath + input_file_name);
        }
        const outputFile = outputPath + output_file_name;
        await doc.save(outputFile, PDFNet.SDFDoc.SaveOptions.e_linearized);
        // output PDF doc
        console.log("Result saved in " + outputFile);
        console.log("Done.");
      } catch (err) {
        console.log(err);
      }
    };
    // add your own license key as the second parameter, e.g. PDFNet.runWithCleanup(main, 'YOUR_LICENSE_KEY')
    PDFNet.runWithCleanup(main).catch(function(error) {
      console.log('Error: ' + JSON.stringify(error));
    }).then(function(){ PDFNet.shutdown(); });
  };
  exports.runCAD2PDFTest();
})(exports);
// eslint-disable-next-line spaced-comment
//# sourceURL=CAD2PDFTest.js
close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

Join our live demo to learn about use cases & capabilities for WebViewer

Learn more
close