VCF / Vcard generation in template

I try to force the download of a (generated) vcf file (vcard) via template, which worked without problems in Kirby 2, on an updated site with Kirby 3.

templates/contact.php (reduced and static to test):

<?php
header('Content-Type: text/x-vcard');
header('Content-Disposition: attachment; filename="test.vcf"');
?>

BEGIN:VCARD
VERSION:2.1
N:Gump;Forrest;;Mr.
FN:Forrest Gump
ORG:Bubba Gump Shrimp Co.
TITLE:Shrimp Man
TEL;WORK;VOICE:(111) 555-1212
TEL;HOME;VOICE:(404) 555-1212
ADR;WORK;PREF:;;100 Waters Edge;Baytown;LA;30314;United States of America
LABEL;WORK;PREF;ENCODING#QUOTED-PRINTABLE;CHARSET#UTF-8:100 Waters Edge#0D#
 #0ABaytown\, LA 30314#0D#0AUnited States of America
ADR;HOME:;;42 Plantation St.;Baytown;LA;30314;United States of America
LABEL;HOME;ENCODING#QUOTED-PRINTABLE;CHARSET#UTF-8:42 Plantation St.#0D#0A#
 Baytown, LA 30314#0D#0AUnited States of America
EMAIL:forrestgump@example.com
REV:20080424T195243Z
END:VCARD

When I open localhost/contacts/contact on my desktop browser it asks to download test.vcf. It basically works. But when I try to open it from my mobile (iPhone in this case) it only offers to download “test.vcf.htm”, which can be downloaded and opened with the correct content, but is treated as a text/htm file… which is a pain on a mobile device. It should straight up open a contact sheet, as it did with Kirby 2.

When I test it with a plain .php file on the server and call that… it works on the iPhone. Kirby 3 gets in the way somewhere… and I don’t know where :slight_smile:

Any ideas? Thanks!

(Kirby 3.6.2)

I am using this method from the guide for user data exports in .csv or .json format. So this should work with .vcf as well?

Create a contact.vcf.php inside the template folder and copy your existing code from above. (Looks good to me so far, I am just not sure about the second header.)

I hope this helps.

Thank you very much!

It works now with the following setup:

empty site/templates/contact.php
My code in site/templates/contact.vcf.php

First header didn’t work, but with $kirby->response() it seems to work everywhere (even with Umlauts and everything ;)) Second header is not necessary it seems. The .vcf is automatically downloaded and not viewed inline.

<?php
$kirby->response()->type('text/x-vcard');

echo 'BEGIN:VCARD
VERSION:2.1
N:Gump;Forrest;;Mr.
FN:Forrest Gump
ORG:Bubba Gump Shrimp Co.
TITLE:Shrimp Man
TEL;WORK;VOICE:(111) 555-1212
TEL;HOME;VOICE:(404) 555-1212
ADR;WORK;PREF:;;100 Waters Edge;Baytown;LA;30314;United States of America
LABEL;WORK;PREF;ENCODING#QUOTED-PRINTABLE;CHARSET#UTF-8:100 Waters Edge#0D#
 #0ABaytown\, LA 30314#0D#0AUnited States of America
ADR;HOME:;;42 Plantation St.;Baytown;LA;30314;United States of America
LABEL;HOME;ENCODING#QUOTED-PRINTABLE;CHARSET#UTF-8:42 Plantation St.#0D#0A#
 Baytown, LA 30314#0D#0AUnited States of America
EMAIL:forrestgump@example.com
REV:20080424T195243Z
END:VCARD';
?>
1 Like