25

There are several ways of setting PDF meta data when compiling LaTeX documents. The two most popular ways are arguably via pdfinfo,

\pdfinfo{
   /Author (Erwin Schrödinger)
}

and hyperref,

\usepackage[pdftex,
  pdfauthor={Erwin Schrödinger},
]{hyperref}

With both methods, however, the above example produces mojibake in the PDF output:

evince screenshot

$ pdfinfo main.pdf
Title:          
Subject:        
Keywords:       
Author:         Erwin Schrödinger
[...]

Adding \usepackage[utf8]{inputenc} makes no difference.

How to fix this?

3
  • you need unicode option in hyperref to use utf8 in pdf metadada fields May 20, 2015 at 10:56
  • Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem.
    – Sverre
    May 20, 2015 at 11:40
  • @DavidCarlisle ö is available in PDFDocEncoding. But LaTeX expands the option before passing it to hyperref without giving hyperref the chance to setup the macros for the PDF string conversion and changing the font encoding. May 20, 2015 at 11:45

1 Answer 1

28

Package hyperref

hyperref encodes correctly, but the options should be set after hyperref is loaded. Otherwise LaTeX expands the options the hard way and hyperref will only see the expanded garbage.

\usepackage{hyperref}
\hypersetup{
  pdfauthor={Erwin Schrödinger},
}

Extended example:

\documentclass{article}
\usepackage[utf8]{inputenc}% utf8, for example
\usepackage[
  pdfencoding=auto,% or unicode
  psdextra,
]{hyperref}
\hypersetup{
  pdfauthor={Erwin Schrödinger},
}
\begin{document}
  \null
\end{document}

The meta data strings are encoded in PDFDocEncoding or UTF-16BE with BOM in the PDF file. Full power of Unicode for the meta data and bookmarks are enabled by the following hyperref options:

\usepackage[
  pdfencoding=auto,% or unicode
  psdextra,
]{hyperref}

pdfencoding=auto is more flexible than unicode. If the string fits the PDFDocEncoding (an 8-bit encoding), then this encoding is used, otherwise Unicode.

Low level, manually

The low level version for specifying the meta data with pdfTeX and without hyperref would look like:

\pdfinfo{/Author(Erwin Schr\string\366dinger)}

The Unicode variants:

\begingroup
  \escapechar=`\\
  \edef\.{\string\000}%
  \pdfinfo{/Author(\string\376\string\377% BOM
    \.E\.r\.w\.i\.n\. \.S\.c\.h\.r\.\string\366\.d\.i\.n\.g\.e\.r)}%
\endgroup

or as hexadecimal string: \pdfinfo{/Author}

The PDF specification tells, which encoding can be used, PDFDocEncoding is listed as full table in Annex D "Character Sets and Encodings".

Low level, but with automatic encoding conversions

If you want to convert from UTF-8 (or other input encoding), then package stringenc helps (LaTeX and plain TeX formats): Plain TeX example:

% UTF-8 encoded source file
\input stringenc.sty\relax

\def\MyAuthor{Erwin Schrödinger}
\edef\BOM{\string\376\string\377}
\StringEncodingConvert{\PdfAuthor}{\MyAuthor}{utf8}{utf16be}
\StringEncodingSuccessFailure{}{%
  \errmessage{Conversion from utf8 to utf16be failed for author string}%
}
\pdfinfo{/Author(\BOM\pdfescapestring{\PdfAuthor})}%
\null
\bye

A more elaborate example, reimplementing pdfencoding=auto:

% UTF-8 encoded source file
\input stringenc.sty\relax

\def\GeneratePdfString#1#2{%
  \StringEncodingConvertTest{#1}{#2}{utf8}{pdfdoc}{%
    % Success: \PdfAuthor with PDFDocEncoding
    % Make full PDF string inclusive brackets
    \edef#1{(\pdfescapestring{#1})}%
  }{%
    \StringEncodingConvert{#1}{#2}{utf8}{utf16be}%
    \StringEncodingSuccessFailure{%
      % Success: \PdfAuthor with UTF-16BE
    }{%
      \errmessage{Conversion from utf8 to utf16be failed for author string}%
    }%
    % Make full PDF string with BOM for case UTF-16BE
    \edef#1{(\BOM\pdfescapestring{#1})}%
  }%
}
\edef\BOM{\string\376\string\377}%

% Usage
\def\MyAuthor{Erwin Schrödinger}
\GeneratePdfString\PdfAuthor{\MyAuthor}
\GeneratePdfString\PdfTitle{Unicode string example ☺}

\pdfinfo{%
  /Author\PdfAuthor
  /Title\PdfTitle
}%
\null
\bye

The input need not to be encoding in UTF-8, package stringenc supports much more encodings.

4
  • \hypersetup{pdfauthor={Erwin Schrödinger}} doesn not work for me, \hypersetup{pdfauthor={Erwin Schr\"odinger}} does. May 20, 2015 at 11:09
  • @NicoSchlömer Then you haven't (yet) loaded package inputenc with the right option. May 20, 2015 at 11:46
  • Is there a latex3 way to do the same thing that is proposed with stringenc.sty
    – callegar
    Sep 27, 2020 at 12:40
  • @Callegar When I implemented the package I did not want to limit the usability of the package to a sole TeX format; the package has a topic that is unrelated to a TeX format. Therefore, the package can be used, for example, in LaTeX, plain TeX, even iniTeX ("one package to rule them all"). Sep 28, 2020 at 18:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .