Convert CAD to PDF - CAD2PDF - Python Sample Code

Requirements
View Demo

Sample code demonstrates how to use the Apryse CAD module for direct, high-quality conversion from DWG, DXF, DGN, DWF, and RVT to PDF. Samples provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, Go and VB.

To convert CAD to PDF with Apryse SDK with this sample, you will need:

  1. Get started with Server SDK in your language/framework
  2. Download the CAD Module
  3. Add the sample code provided below

To use this feature in production, your license key will need the CAD Conversion Package. Trial keys already include all packages.

Learn more about our Server SDK and PDF Conversion Library.

1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11sys.path.append("../../LicenseKey/PYTHON")
12from LicenseKey import *
13
14# Relative path to the folder containing test files.
15input_path = "../../TestFiles/CAD/"
16output_path = "../../TestFiles/Output/"
17
18# ---------------------------------------------------------------------------------------
19# The following sample illustrates how to use CAD module
20# --------------------------------------------------------------------------------------
21
22def main():
23
24 # The first step in every application using PDFNet is to initialize the
25 # library and set the path to common PDF resources. The library is usually
26 # initialized only once, but calling Initialize() multiple times is also fine.
27 PDFNet.Initialize(LicenseKey)
28
29 # The location of the CAD Module
30 PDFNet.AddResourceSearchPath("../../../PDFNetC/Lib/")
31
32 if not CADModule.IsModuleAvailable():
33
34 print("""
35 Unable to run CAD2PDFTest: PDFTron SDK CAD module not available.
36 ---------------------------------------------------------------
37 The CAD module is an optional add-on, available for download
38 at https://dev.apryse.com/. If you have already downloaded this
39 module, ensure that the SDK is able to find the required files
40 using the PDFNet::AddResourceSearchPath() function.""")
41
42 else:
43
44 inputFileName = "construction drawings color-28.05.18.dwg"
45 outputFileName = inputFileName + ".pdf"
46 doc = PDFDoc()
47 Convert.FromCAD(doc, input_path + inputFileName, None)
48 doc.Save(output_path + outputFileName, 0)
49
50 PDFNet.Terminate()
51 print("CAD2PDF conversion example")
52
53
54if __name__ == '__main__':
55 main()

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales