6

Say I have an image image1.jpg
I can encode it using base64 tool :

myImgStr=$(base64 image1.jpg)

I tried to decode it using the following command:

base64 -i -d $myImgStr > image2.jpg

or

echo -n $myImgStr | base64 -d -i > image2.jpg

But in both cases I get the following error:

base64: extra operand ‘/9j/4AAQSkZJRgABAQAAAQABAAD/7QCEUGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAGgcAmcAFHNH’ Try 'base64 --help' for more information.
Any help is appreciated.

15

The utility base64 reads its input either from a file whose name is supplied as an argument, or from standard input. It never reads its input from a command line argument. In your case, to decode a string stored in a variable, you should supply the string on the standard input of base64.

If you are using Bash, you may use a here-string:

base64 -d <<< "$myImgStr" > image2.jpg

If your shell does not accept here-strings, you can always use:

echo "$myImgStr" | base64 -d > image2.jpg

(Note the doublequotes around "$myImgStr". You should always doublequote variable expansions unless you have a good reason not to.)

  • 1
    Exactly what I wanted to write. In the second version, it might be useful to emphasize how the variable is enclosed in double quotes ("$myImgStr"), which is necessary and missing in the failed attempts listed in the question. – Byte Commander Apr 21 '17 at 21:07
  • Why you used <<< instead of << in the first command? – Navaro Apr 21 '17 at 21:12
  • @Navaro: << and <<< are different things. << introduces a here-document, and <<< a here-string. See man bash; search for <<<. – AlexP Apr 21 '17 at 21:14
  • I think it is -D not -d – Lucas Goossen Aug 24 '18 at 21:07
  • @LucasGoossen: manual page for base64(1). – AlexP Aug 24 '18 at 21:30
0

How to export the Salesforce Quote PDF file and read convert it from Base64 back to PDF using Linux command Line

  1. Use Dataloader and export the QuoteDocument object with all fields and data

dataloader1

dataloader2

dataloader3

  2. Use text editor, TextPad to open the csv file. Don’t use Notepad because it cannot handle the large size data and truncates it. 3. TextPad can handle the large data and respects any newlines characters, etc., when you open the file and also when you copy/paste the data. 4. Go to a specific row in the data and select and copy the cell field string that contains the PDF Base64 encoded data. Make sure you select it all the way to the end – but do not include subsequent fields after it.

textpad1

  1. Do not include subsequent fields after it.
  2. Select the string up to but do not include the quote " symbol.

textpad2

  1. Copy/paste the string into a new TextPad window.
  2. Save the new TextPad to a file, for example, document1.txt
  3. Copy the docuemnt1.txt file to your Linux computer (you could use Dropbox for this).
  4. Open a Linux command line terminal window
  5. Run the base64 decode command (base64 is part of the coreutils package), $ base64 --decode ~/Dropbox/linux_stuff/Document1.txt > ~/Dropbox/linux_stuff/decoded1.pdf
  6. This command will read in the Document1.txt file and output the decoded PDF file to decoded1.pdf.
  7. You can now open the decoded1.pdf file as an PDF file.
  8. You can rename the decoded1.pdf file to whatever you want, i.e., back to the original file name that was attached in the original Salesforce Quote object record.

linux_command_line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.