Author Topic: Best way to open pdf in browser in autocad macro  (Read 18485 times)

0 Members and 1 Guest are viewing this topic.

Keith Brown

  • Swamp Rat
  • Posts: 601
Best way to open pdf in browser in autocad macro
« on: June 13, 2013, 09:04:54 AM »
I have a need to have several buttons on my ribbon that will open up different pdfs.  I would like to be able to open up to certain bookmarks in the pdf and the only way I know that autocad can do that is by opening up the pdf in a browser with a url like this - c:/filename.pdf#view=FitV&page=10.  So in my macro I have the following:
 
Code - Lisp: [Select]
  1. ^C^C^P(command "._browser" "C:/Version_5.2.pdf#view=FitV")

My question to ask is if there is a better LISP function that will do the same thing?  Or for my purposes will this work ok?  I have heard that there is issues with the browser command but I have not experienced it yet.  I work off of IExplorer but the ribbon buttons has the possibility to be used by any browser and I would rather find out sooner than later that the macro I am using will work for everything.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Best way to open pdf in browser in autocad macro
« Reply #1 on: June 13, 2013, 09:10:29 AM »
Try:
Code: [Select]
^C^C(startapp "explorer" "C:/Version_5.2.pdf")

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Best way to open pdf in browser in autocad macro
« Reply #2 on: June 13, 2013, 09:14:47 AM »
That doesn't work very well.  It opens up the My Documents window and just prints a 33 to the command line.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Best way to open pdf in browser in autocad macro
« Reply #3 on: June 13, 2013, 09:15:36 AM »
That doesn't work very well.  It opens up the My Documents window and just prints a 33 to the command line.

If the file can be found, it will open the file using the default program associated with the file extension.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: Best way to open pdf in browser in autocad macro
« Reply #4 on: June 13, 2013, 09:17:39 AM »
Google chrome will open a PDF too.
TheSwamp.org  (serving the CAD community since 2003)

GDF

  • Water Moccasin
  • Posts: 2081
Re: Best way to open pdf in browser in autocad macro
« Reply #5 on: June 13, 2013, 09:23:11 AM »
(command "start" (strcat yourpathhere "ARCH_FILE.PDF"))
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Best way to open pdf in browser in autocad macro
« Reply #6 on: June 13, 2013, 09:28:19 AM »
That doesn't work very well.  It opens up the My Documents window and just prints a 33 to the command line.

If the file can be found, it will open the file using the default program associated with the file extension.

It appears to not be working on my machine.  The explorer window opens up to C:\Users\Keith\Documents and the pdf doesn't open.  A 33 is printed to the command window.  A copy/paste of my actual command is:
Code - Text: [Select]
  1. ^C^C(startapp "explorer" "c:/version_5.2.pdf")

and this is what gets printed to the command line. 
Code - Text: [Select]
  1. Command: (startapp "explorer" "c:/version_5.2.pdf") 33
and I have triple checked the file and it is named correctly and located in my root drive.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: Best way to open pdf in browser in autocad macro
« Reply #7 on: June 13, 2013, 09:29:41 AM »
Google chrome will open a PDF too.

although i'm not having any luck making it work with 'startapp'. :(
TheSwamp.org  (serving the CAD community since 2003)

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Best way to open pdf in browser in autocad macro
« Reply #8 on: June 13, 2013, 09:35:08 AM »
(command "start" (strcat yourpathhere "ARCH_FILE.PDF"))

While this will work to open up a pdf.  It will not work for my purpose.  I need to have the pdf open up to a certain page and/or bookmark using the following syntax:
 
Code - Text: [Select]
  1. Version_5.2.pdf#View=FitV&nameddest=Chapter3

That is the reason why I was using the browser command.  Forcing it open in a browser session will respect the parameters after the pdf filename and file extension.  I just thought there was a better way to open a browser url.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Best way to open pdf in browser in autocad macro
« Reply #9 on: June 13, 2013, 09:40:07 AM »
I just thought there was a better way to open a browser url.

Maybe try something like this:
Code - Auto/Visual Lisp: [Select]
  1. (defun openpdf ( pdf / exe pth )
  2.     (setq pth (LM:expenvstr "%PROGRAMFILES%")
  3.           pdf (strcat "\"" (vl-string-translate "/" "\\" pdf) "\"")
  4.     )
  5.     (cond
  6.         (   (and (setq exe (findfile (strcat pth "\\Mozilla Firefox\\firefox.exe")))
  7.                  (startapp exe pdf)
  8.             )
  9.         )
  10.         (   (and (setq exe (findfile (strcat pth "\\Internet Explorer\\iexplore.exe")))
  11.                  (startapp exe pdf)
  12.             )
  13.         )
  14.     )
  15. )
  16.  
  17. ;; Expand Environment String  -  Lee Mac
  18. (defun LM:expenvstr ( str / res wsh )
  19.     (if (setq wsh (vlax-create-object "wscript.shell"))
  20.         (progn
  21.             (setq res (vl-catch-all-apply 'vlax-invoke (list wsh 'expandenvironmentstrings str)))
  22.             (vlax-release-object wsh)
  23.             (if (null (vl-catch-all-error-p res))
  24.                 res
  25.             )
  26.         )
  27.     )
  28. )

You can add extra conditions to account for other browsers (I don't know the path to the Chrome executable), and you can also change the order of conditions to use a preferred browser.

Call with:
Code - Auto/Visual Lisp: [Select]
  1. (openpdf "C:/Version_5.2.pdf#View=FitV&nameddest=Chapter3")

JohnK

  • Administrator
  • Seagull
  • Posts: 10625
Re: Best way to open pdf in browser in autocad macro
« Reply #10 on: June 13, 2013, 09:40:38 AM »
I would use the first method (the requirements are to open a PDF to a specific location).

EDIT:
You guys type fast.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Best way to open pdf in browser in autocad macro
« Reply #11 on: June 13, 2013, 09:53:15 AM »
I just thought there was a better way to open a browser url.

Actually, this might be better:

Code - Auto/Visual Lisp: [Select]
  1. (defun browser ( url )
  2.     (setq url (strcat "\"" (vl-string-translate "/" "\\" url) "\""))
  3.     (vl-some '(lambda ( exe ) (and (findfile exe) (startapp exe url)))
  4.         (list
  5.             (strcat (getenv "programfiles") "\\Mozilla Firefox\\firefox.exe")
  6.             (strcat (getenv "localappdata") "\\Google\\Chrome\\Application\\chrome.exe")
  7.             (strcat (getenv "programfiles") "\\Internet Explorer\\iexplore.exe")
  8.         )
  9.     )
  10. )
Code - Auto/Visual Lisp: [Select]
  1. (browser "C:/Version_5.2.pdf#View=FitV&nameddest=Chapter3")
« Last Edit: June 13, 2013, 10:11:20 AM by Lee Mac »

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Best way to open pdf in browser in autocad macro
« Reply #12 on: June 13, 2013, 09:53:35 AM »
You could use this to open in chrome ... does not check if chrome is installed.


Code: [Select]
(defun _openfileinchrome (file / sa)
  (and (findfile file)
       (setq sa (vlax-get-or-create-object "Shell.Application"))
       (null (vlax-invoke sa 'shellexecute "chrome.exe" file))
       (vlax-release-object sa)
  )
)
(_openfileinchrome "c:\\test.pdf")
« Last Edit: June 13, 2013, 10:15:37 AM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Best way to open pdf in browser in autocad macro
« Reply #13 on: June 13, 2013, 10:38:09 AM »
I just thought there was a better way to open a browser url.

Actually, this might be better:

Code - Auto/Visual Lisp: [Select]
  1. (defun browser ( url )
  2.     (setq url (strcat "\"" (vl-string-translate "/" "\\" url) "\""))
  3.     (vl-some '(lambda ( exe ) (and (findfile exe) (startapp exe url)))
  4.         (list
  5.             (strcat (getenv "programfiles") "\\Mozilla Firefox\\firefox.exe")
  6.             (strcat (getenv "localappdata") "\\Google\\Chrome\\Application\\chrome.exe")
  7.             (strcat (getenv "programfiles") "\\Internet Explorer\\iexplore.exe")
  8.         )
  9.     )
  10. )
Code - Auto/Visual Lisp: [Select]
  1. (browser "C:/Version_5.2.pdf#View=FitV&nameddest=Chapter3")

This works great Lee.  Thanks.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Best way to open pdf in browser in autocad macro
« Reply #14 on: June 13, 2013, 10:40:15 AM »
I only have IE on  my machine.  I am wondering how the order of the list will effect uses that have ie, firefox, and chrome?
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013