VBA to decrypt pdf with PDFtk server

prati

Board Regular
Joined
Jan 25, 2021
Messages
51
Office Version
  1. 2019
Platform
  1. Windows
Hey,
I'm using PDFtk server for several tasks dealing with pdf files.

According to the manual and the examples - PDFtk Server Examples
there should be a way to decrypt pdf files.

Decrypt a PDF

pdftk secured.pdf input_pw foopass output unsecured.pdf

There is also a possibility to merge 2 pdf files that one of the is encrypted

Join two files, one of which requires the password foopass. The output is not encrypted.

pdftk A=secured.pdf 2.pdf input_pw A=foopass cat output 3.pdf

------------------------------------------------------------------------------------------------------------------------------------------

A.pdf is the encrypted file I want to decrypt
The password for A.pdf is abc123def
B.pdf should be the new file without encryption

I created the VBA below, however it doesn't work.

------------------------------------------------------------------------------------------------------------------------------------------
Sub Decrypt_Pdf()

Dim Wsh As Object 'WshShell
Set Wsh = CreateObject("WScript.Shell") 'New WshShell
Dim s As String

s = "cmd /c PDFtk " & "C:\Temp\A.pdf " & input_pw & "abc123def" & " output C:\Temp\B.pdf"

'Debug.Print 3, s
Wsh.Run s, 0, True

End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
It is very likely that the command line is incorrect.
Cmd.exe can be found because the PATH environment variable of the Windows OS contains an entry to the folder where it resides on default. It's very unlikely the same to be true for the PDFtk executable. It is therefore necessary to enter its full path.
I also notice an inconsistency in the parameter line of the PDFtk executable. As it looks now, you specify a VBA variable called input_pw as the second argument. It is not declared explicitly within your procedure, nothing is assigned to it, so this variable of memory type Variant is most likely to end up containing nothing (vbEmpty).
Would therefore recommend to run a visible shell using Wsh.Run s, 1, True. It enables you to examine what the final command line looks like. And more importantly, whether there are warnings or error messages. The moment everything appears to run smoothly you can hide the shell again.
 
Upvote 0
I already assumed that. By the way, you don't have to use concatenations. You can just write it one after the other in one go.
VBA Code:
s = "cmd /c PDFtk  C:\Temp\A.pdf  input_pw  abc123def  output  C:\Temp\B.pdf"
 
Upvote 0
Solution

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top