예제 #1
0
파일: pdf.py 프로젝트: tgulacsi/apostle
def any2pdf(stream, typ='application/octet-stream'):
    if gdrive:
        res = gdrive.to_pdf(stream, typ=typ)
        if res:
            return res
    if typ == 'application/octet-stream' or typ.startswith('image/'):
        try:
            res = image2pdf_gm(stream)
            if res:
                return res
        except:
            # LOG.exception('tried GM, but seems to be not image')
            LOG.warn('tried GM, but seems to be not image')
            stream = _truncate(stream)
    return office_pdf(stream)
예제 #2
0
파일: pdf.py 프로젝트: tgulacsi/apostle
def plaintext2pdf(stream):
    text = (stream.read(256 * 1024) if hasattr(stream, 'read')
        else stream[:256 * 1024])
    if not isinstance(text, basestring):
        text = str(text)
    if isinstance(text, str):
        ok = False
        for error in ('strict', 'replace'):
            for encoding in ('utf8', 'iso8859_2', 'cp1250', 'ibm852',
                    'iso8859_1'):
                try:
                    text = unicode(text, encoding, error)
                    ok = True
                    break
                except UnicodeDecodeError:
                    pass
            if ok:
                break
    try:
        return rl_build_story([rl_paragraph(para)
            for para in text.splitlines()])
    except:
        LOG.exception('cannot convert %s with ReportLab', text[:1024])
        return office_pdf(text)