Convert Microsoft Visio Drawing (vsd) to PDF automatically
Asked Answered
M

5

6

An open-source project I am working on uses Visio drawings for documentation, which are checked into source control. For those working on the project who don't own Visio, we have been converting the vsd files to PDFs so that they can still view them. It's not too difficult to save a copy as a PDF when making changes to the documentation, but we would like an automated way to do this conversion, so that we can set it up as a pre-checkin script in the SVN client. If anybody knows of a way to do this, either using something built-in to Visio, or with an outside script or command line tool, we would appreciate it.

Edit: Thanks to the suggestion below, I have found the Visio Viewer 2010. This will be helpful for our contributors using Windows. We would still like to have the ability to create PDFs though, as there are readers available on every major operating system, and our contributors will not be using only Windows.

Mistral answered 24/12, 2010 at 1:37 Comment(3)
Look around on the net. There used to be a Visio Viewer application free from Microsoft.Cement
@JohnS, thanks for the tip; I'll look around. Do you recall if it was available for OS X? The project is a web application, and some of our contributors are using OS X as their primary OS.Mistral
I think the Visio viewer is an ActiveX control, so it would be Windows only. I could be wrong.Aroma
C
5

I found this nice vbs script and adapted it to visio.It can be called via cygwin (works for all kind of Office stuff)

 Option Explicit

Main()

Sub Main()
  If WScript.Arguments.Count > 0 Then 
      Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
      Dim i
      For i = 0 to wscript.arguments.count - 1
          Dim strFilePath : strFilePath = WScript.Arguments.Item(i)
          Dim dirPath : dirPath = objFSO.GetParentFolderName(strFilePath)
          Dim fileBaseName : fileBaseName = objFSO.GetBaseName(strFilePath)
          'WScript.Echo strFilePath
          Dim strNewFileName : strNewFileName = dirPath & "\" & fileBaseName & ".pdf"
          'WScript.Echo strNewFileName
          Dim strFileExt : strFileExt = UCase(objFSO.GetExtensionName(strFilePath))
          Select Case strFileExt
              Case "DOC"
                  DOC2PDF strFilePath, strNewFileName
              Case "XLS"
                  XLS2PDF strFilePath, strNewFileName
              Case "PPT"
                  PPT2PDF strFilePath, strNewFileName
              Case "VSD"
                  VSD2PDF strFilePath, strNewFileName
              Case Else
                  WScript.Echo "Extension Type:  " & strFileExt
          End Select
      Next
  Else
      msgbox("Sie muessen eine Datei zum konvertieren auswählen.")
  End If
End Sub

Sub PPT2PDF(strSourceFile, strDestFile)
  Const ppWindowMinimized = 2
  Const ppWindowNormal = 1
  Const ppSaveAsPDF = 32

  Dim objPPT : Set objPPT = CreateObject("PowerPoint.Application")
  objPPT.Visible = True
  objPPT.WindowState = ppWindowMinimized
  objPPT.Presentations.Open strSourceFile
  objPPT.ActivePresentation.SaveAs strDestFile, ppSaveAsPDF 
  objPPT.Quit()
End Sub

Sub DOC2PDF(strSourceFile, strDestFile)
  Const wdExportAllDocument = 0
  Const wdExportOptimizeForPrint = 0
  Const wdExportDocumentContent = 0
  Const wdExportFormatPDF = 17
  Const wdExportCreateHeadingBookmarks = 1

  Dim objWord : Set objWord = CreateObject("Word.Application")
  Dim objDoc : Set objDoc = objWord.Documents.Open(strSourceFile,,TRUE)    
  objWord.ActiveDocument.ExportAsFixedFormat strDestFile, wdExportFormatPDF, False, _
                  wdExportOptimizeForPrint, wdExportAllDocument,,, _
                  wdExportDocumentContent, False, True, wdExportCreateHeadingBookmarks
  objWord.Quit()
End Sub

Sub XLS2PDF(strSourceFile, strDestFile)
  Const xlTypePDF = 0

  Dim objExcel : Set objExcel = CreateObject("Excel.Application")
  Dim objeDoc : Set objeDoc = objExcel.Workbooks.Open(strSourceFile,,TRUE)    
  objExcel.ActiveWorkbook.ExportAsFixedFormat xlTypePDF, strDestFile
  objExcel.ActiveWorkbook.Close(False)
  objExcel.Quit
End Sub

Sub VSD2PDF(strSourceFile, strDestFile)
  Const xlTypePDF = 1
  Const visOpenRO = 2
  Const visOpenMinimized = 16
  Const visOpenHidden = 64
  Const visOpenMacrosDisabled = 128
  Const visOpenNoWorkspace = 256

  Dim objVisio : Set objVisio = CreateObject("Visio.Application")
  Dim objeDoc : Set objeDoc = objVisio.Documents.OpenEx(strSourceFile, visOpenRO + visOpenMinimized + visOpenHidden + visOpenMacrosDisabled + visOpenNoWorkspace)    
  objeDoc.ExportAsFixedFormat xlTypePDF, strDestFile, 1, 0
  objeDoc.Close
  objVisio.Quit
End Sub
Chatterjee answered 5/10, 2011 at 13:52 Comment(1)
initial script source w/o visio: visualbasicscript.com/fb.ashx?m=78182Chatterjee
S
2

You could use vsd2svg and svg2pdf for the conversion process - if you would like to do it on the commandline. Or the underlying libraries.

http://dia-installer.de/vsd2svg

http://cgit.freedesktop.org/~cworth/svg2pdf/

Synecdoche answered 6/9, 2012 at 20:6 Comment(0)
A
1

Visio 2007 added the ExportAsFixedFormat method to the Visio API that will let you programmatically create a PDF file in Visio.

The tricky part is deploying the code in a way that makes sense for your project. You could create a stencil that just acts as an addon, and attach that stencil to all the Visio documents in your project. The code in the stencil would just watch the Visio application object for DocumentSaved events, and save a pdf of the document in the same folder.

Aroma answered 29/12, 2010 at 20:5 Comment(0)
N
1

This project is really cool : https://github.com/cognidox/OfficeToPDF

Super easy to integrate to a Python process task. The compiled version (OfficeToPDF.exe) is provided.

Nace answered 24/11, 2018 at 2:5 Comment(0)
E
0

I wrote a small Python script to do this:

import pathlib
import sys
import win32com.client


def set_ps(page, cell, value):
    page.PageSheet.Cells(cell).Formula = value


def convert(visio, path, out_path):
    doc = visio.Documents.Open(str(path))

    for page in doc.Pages:
        set_ps(page, "PageLeftMargin", "0mm")
        set_ps(page, "PageRightMargin", "0mm")
        set_ps(page, "PageTopMargin", "0mm")
        set_ps(page, "PageBottomMargin", "0mm")
        page.ResizeToFitContents()

    pdf_format = 1
    intent_print = 1
    print_all = 0
    doc.ExportAsFixedFormat(pdf_format, out_path, intent_print, print_all)
    doc.Close()


def main():
    visio = win32com.client.Dispatch("Visio.Application")
    visio.AlertResponse = 7  # Answer "no" to all save dialogs

    basedir = pathlib.Path(sys.argv[1])
    files = list(basedir.glob('*.vsdx'))
    for i, path in enumerate(files, start=1):
        print(f"[{i:3}/{len(files):3}] {path.stem}")
        out_path = path.with_suffix(path.suffix + '.pdf')
        if out_path.exists():
            continue
        convert(visio, path, out_path)


if __name__ == '__main__':
    main()

You'll need Python and the pywin32 library, then run it with a path to a folder containing .vsdx files. It might work with .vsd as well, though I didn't test that.

Envisage answered 19/3, 2020 at 16:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.