@classmethod
def from_unicode(cls, source, text_encoding='auto', bytes_encoding='auto'):
    """ The from_unicode() constructor is called to encode a source
            string into a PdfString that is suitable for inclusion in a PDF.

            NOTE:  There is no magic in the encoding process.  A user
            can certainly do his own encoding, and simply initialize a
            PdfString() instance with his encoded string.  That may be
            useful, for example, to add line breaks to make it easier
            to load PDFs into editors, or to not bother to escape balanced
            parentheses, or to escape additional characters to make a PDF
            more readable in a file editor.  Those are features not
            supported by this method.

            from_unicode() can use a heuristic to figure out the best
            encoding for the string, or the user can control the process
            by changing the text_encoding parameter to 'pdfdocencoding'
            or 'utf16', and/or by changing the bytes_encoding parameter
            to 'literal' or 'hex' to force particular conversion methods.

            The function will raise an exception if it cannot perform
            the conversion as requested by the user.
        """
    if text_encoding != 'utf16':
        force_pdfdoc = text_encoding == 'pdfdocencoding'
        if text_encoding != 'auto' and not force_pdfdoc:
            raise ValueError('Invalid text_encoding value: %s' % text_encoding)
        if source.startswith(cls.bad_pdfdoc_prefix):
            if force_pdfdoc:
                raise UnicodeError(
                    'Prefix of string %r cannot be encoded in pdfdocencoding' %
                    source[:20])
        else:
            try:
                raw = source.encode('pdfdocencoding')
            except UnicodeError:
                if force_pdfdoc:
                    raise
            else:
                return cls.from_bytes(raw, bytes_encoding)
    raw = cls.bytes_bom + source.encode('utf-16-be')
    encoding = 'hex' if bytes_encoding == 'auto' else bytes_encoding
    return cls.from_bytes(raw, encoding)
