Files
shell_bitwarden_json_to_html/bitwarden_convert_to_html.sh
2025-02-20 20:49:54 +01:00

151 lines
6.6 KiB
Bash
Executable File

#!/bin/bash
# Pfad zum Verzeichnis mit den JSON-Dateien (aktuelles Verzeichnis)
json_dir="$(pwd)"
# Überprüfen, ob das Verzeichnis existiert
if [ ! -d "$json_dir" ]; then
echo "Verzeichnis nicht gefunden!"
exit 1
fi
# CSS-Variablen für Stile
# Hintergrundfarbe
background_color="#ffffff" # Standard-Hintergrundfarbe (Weiß)
# h1
header1_font_size="24px"
header1_font_size_print="48px"
header1_font_weight="bold"
header1_font_family="Arial, sans-serif"
header1_color="#000000"
# h2
header2_font_size="20px"
header2_font_size_print="40px"
header2_font_weight="bold"
header2_font_family="Arial, sans-serif"
header2_color="#000000"
# strong
strong_font_size="16px"
strong_font_size_print="32px"
strong_font_weight="bold"
strong_font_family="Arial, sans-serif"
strong_color="#000000"
# Normaler Text
normal_font_size="14px"
normal_font_size_print="28px"
normal_font_weight="normal"
normal_font_family="Arial, sans-serif"
normal_color="#555555"
# Margin-Left und Margin-Right für alle Elemente
margin_left="10px"
margin_right="10px" # Hinzugefügt für Konsistenz auf der Webansicht
margin_left_print="30px"
margin_right_print="30px"
# Abstand der Trennstriche zum Text
hr_margin="10px 0" # Beispiel: 10px Abstand oben und unten
hr_margin_print="20px 0" # Erhöhter Abstand für die Druckansicht
# Durchlaufe alle JSON-Dateien im Verzeichnis
for json_file in "$json_dir"/*.json; do
# Überprüfen, ob die Datei existiert
if [ ! -f "$json_file" ]; then
echo "Keine JSON-Dateien gefunden!"
continue
fi
# Ausgabe-HTML-Datei basierend auf dem Namen der JSON-Datei
output_file="${json_file%.json}.html"
# HTML-Kopfzeile mit CSS-Styles schreiben
echo "<html>" > "$output_file"
echo "<head><title>Passwort Manager Daten</title>" >> "$output_file"
echo "<meta charset=\"UTF-8\">" >> "$output_file"
echo "<style>" >> "$output_file"
echo "/* CSS-Reset */" >> "$output_file"
echo "* { margin: 0; padding: 0; box-sizing: border-box; }" >> "$output_file"
echo "body { font-family: $normal_font_family; color: $normal_color; margin-left: $margin_left; margin-right: $margin_right; background-color: $background_color; }" >> "$output_file"
echo "h1 { font-size: $header1_font_size; font-weight: $header1_font_weight; font-family: $header1_font_family; color: $header1_color; margin-bottom: 10px; margin-left: $margin_left; }" >> "$output_file"
echo "h2 { font-size: $header2_font_size; font-weight: $header2_font_weight; font-family: $header2_font_family; color: $header2_color; margin-bottom: 8px; margin-left: $margin_left; }" >> "$output_file"
echo ".strong { font-size: $strong_font_size; font-weight: $strong_font_weight; font-family: $strong_font_family; color: $strong_color; }" >> "$output_file"
echo "p { font-size: $normal_font_size; font-weight: $normal_font_weight; color: $normal_color; margin-bottom: 5px; margin-left: $margin_left; }" >> "$output_file"
echo "p.uri { margin-left: $margin_left_subcategory; }" >> "$output_file"
echo "p.fields { margin-left: $margin_left_subcategory; }" >> "$output_file"
echo "div { margin-bottom: 20px; margin-left: $margin_left; margin-right: $margin_right; }" >> "$output_file"
echo "hr { margin: $hr_margin; height: 2px; border: none; background-color: $normal_color; }" >> "$output_file"
echo "@media print {" >> "$output_file"
echo " body { font-size: $normal_font_size_print; margin-left: $margin_left_print; margin-right: $margin_right_print; }" >> "$output_file"
echo " h1 { font-size: $header1_font_size_print; }" >> "$output_file"
echo " h2 { font-size: $header2_font_size_print; }" >> "$output_file"
echo " .strong { font-size: $strong_font_size_print; }" >> "$output_file"
echo " p { font-size: $normal_font_size_print; }" >> "$output_file"
echo " div { margin-left: $margin_left_print; margin-right: $margin_right_print; }" >> "$output_file"
echo " hr { margin: $hr_margin_print; }" >> "$output_file" # Angepasste Margen für Trennstriche im Druck
echo "}" >> "$output_file"
echo "</style>" >> "$output_file"
echo "</head>" >> "$output_file"
echo "<body>" >> "$output_file"
echo "<h1>Passwort Manager Daten</h1>" >> "$output_file"
# Sammlung-Namen in eine Variable laden
collections=$(jq -r '.collections | map({(.id): .name}) | add' "$json_file")
# Items in HTML umwandeln
jq -r --argjson collections "$collections" '
.items[] |
"<hr/><div><h2>\(.name)</h2>" +
(if .login.username != null then "<p><span class=\"strong\">Nutzername:</span> \(.login.username)</p>" else "" end) +
(if .login.password != null then "<p><span class=\"strong\">Passwort:</span> \(.login.password)</p>" else "" end) +
(if .notes != null then "<p><span class=\"strong\">Notizen:</span> \(.notes)</p>" else "" end) +
(if (.login.uris | length) > 0 then
"<p><span class=\"strong\">URIs:</span></p>" +
(.login.uris | to_entries | map("<p class=\"uri\"><span class=\"strong\">URI \(.key + 1):</span> <a href=\"" + .value.uri + "\">" + .value.uri + "</a></p>") | join(""))
else "" end) +
(if (.fields | length) > 0 then "<p><span class=\"strong\">Benutzerdefinierte Felder:</span></p>" + (.fields | map("<p class=\"fields\"><span class=\"strong\">\(.name):</span> \(.value)</p>") | join("")) else "" end) +
(if (.collectionIds | length) > 0 then
"<p><span class=\"strong\">Sammlungen:</span></p>" +
(.collectionIds | map($collections[.] // "Unbekannt") | sort_by(length) | to_entries | map(
"<p class=\"fields\"><span class=\"strong\">Sammlung \(.key + 1):</span> " +
.value +
"</p>"
) | join(""))
else "" end) +
"</div>"
' "$json_file" >> "$output_file"
# Überprüfen, ob jq einen Fehler hatte
if [ $? -ne 0 ]; then
echo "Fehler beim Parsen der JSON-Datei: $json_file"
continue
fi
# HTML-Fußzeile schreiben
echo "</body>" >> "$output_file"
echo "</html>" >> "$output_file"
echo "HTML-Datei wurde erstellt: $output_file"
# PDF-Datei basierend auf der HTML-Datei erstellen
pdf_file="${output_file%.html}.pdf"
# Überprüfen, ob wkhtmltopdf installiert ist
if ! command -v wkhtmltopdf &> /dev/null; then
echo "PDF Erzeugung nicht möglich. Installieren sie wkhtmltopdf wenn sie Ausgabe in PDF Form benötigen."
exit 1
fi
wkhtmltopdf --page-size A4 --dpi 600 "$output_file" "$pdf_file"
# Überprüfen, ob wkhtmltopdf erfolgreich war
if [ $? -ne 0 ]; then
echo "Fehler beim Erstellen der PDF-Datei: $pdf_file"
else
echo "PDF-Datei wurde erstellt: $pdf_file"
fi
done