I'm trying to extract tables out of a pdf using tabula and powershell. When I enter the command directly in the powershell console, I get the expected result displayed (in utf8 with umlaut-symbols)

java -jar "./tabula-java/$tabulaVersion" --spreadsheet -a 114,53,180,556 "./table.pdf"

But when I put it in a string variable and then write to a file the umlaut symbols become gibberish

$text = java -jar "./tabula-1.0.1-jar-with-dependencies.jar" --spreadsheet -a 114,53,180,556 "./table.pdf"   
Set-Content -Path "./file.txt" -Value $text

Even if I print the variable in the console, the umlaut-symbols are not properly displayed

$text = java -jar "./tabula-1.0.1-jar-with-dependencies.jar" --spreadsheet -a 114,53,180,556 "./table.pdf"   
Write-Output $text  

Is there a way to store it in a string variable (and therefore being able to manipulate the content) and write it to a file with keeping the utf8 (without BOM) encoding?

Using the approach from https://stackoverflow.com/a/5596984/1786528 does not work for me either

$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
[System.IO.File]::WriteAllLines($filepath, $text, $Utf8NoBomEncoding)

I don't get an error, but also no file is created or line added.


[System.IO.File]::WriteAllLines creates a file (in UTF without BOM), I just used a relative path and did not set [System.Environment]::CurrentDirectory = (Get-Location).Path. But nonetheless are the umlaut-symbols not correct.

Additional details

case 1: output directly in console, e.g.

java -jar "./tabula-1.0.1-jar-with-dependencies.jar" --spreadsheet "./table.pdf" 

case 2: output stored in variable, then printed in console, e.g.

$text = java -jar "./tabula-1.0.1-jar-with-dependencies.jar" --spreadsheet "./table.pdf"   
Write-Output $text 

case 3: output stored in variable but with -D"file.encoding=UTF-8", then printed in console, e.g.

$text = java -D"file.encoding=UTF-8" -jar "./tabula-1.0.1-jar-with-dependencies.jar" --spreadsheet "./table.pdf"   
Write-Output $text 


$OutputEncoding = US-ASCII and [System.Console]::OutputEncoding = OEM United States (IBM437)

case 4: output directly in console (with changing [System.Console]::OutputEncoding beforehand), e.g.

[System.Console]::OutputEncoding = System.Text.Encoding]::GetEncoding(1252)
java -jar "./tabula-1.0.1-jar-with-dependencies.jar" --spreadsheet "./table.pdf" 

case 5: output stored in variable, then printed in console (with changing [System.Console]::OutputEncoding beforehand), e.g.

[System.Console]::OutputEncoding = System.Text.Encoding]::GetEncoding(1252)
$text = java -jar "./tabula-1.0.1-jar-with-dependencies.jar" --spreadsheet "./table.pdf"   
Write-Output $text 

This will result in this for umlaut symbols

pdf    case 1    case 2     case 3    case 4     case 5
 ä      ä         Σ          ├ñ        „          ä
 ö      ö         ÷          ├╢        ”          ö
 ü      ü         ⁿ          ├╝                  ü
  • you could try $text = java -jar "./tabula-java/$tabulaVersion" --spreadsheet -a 114,53,180,556 $filepath | out-file -FilePath "./file.txt" -Encoding UTF8 if it does not work, the output ist most likely already wrong – SimonS Apr 13 '18 at 6:38
  • Unfortunately, it doesn't. I also think that the culprit is at $text = java ... (see my update). But I do not understand why the response is displayed correctly when I just use java ... and as soon as I store it in a variable not anymore. It also creates a working file if I write the output directly with the tabula command (using -o "./file.txt") – josh21 Apr 13 '18 at 6:53
  • look at this answer on stack overflow - looks like you can specify UTF8 encoding already in java ... stackoverflow.com/questions/6733029/… adding -Dfile.encoding=UTF-8should do the trick – SimonS Apr 13 '18 at 6:57
  • This sounded promising, but I get this error message Error: Could not find or load main class .encoding=UTF-8. I tried to find more about -Dfile, but not very successfully – josh21 Apr 13 '18 at 7:31
  • hmm i guess you have to wait for a java commandline pro. you should search for java commandline output UTF8 or probably even java tabula UTF8 encoding. Can't help any further since I don't know too much about java (maybe readme.md in github helps) – SimonS Apr 13 '18 at 7:50

Your Answer

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

Browse other questions tagged or ask your own question.