def convert_to_pdf(message):
    #print(message)
    bot.send_message(message.chat.id, 'Converting...')
    send_photo_warning = False
    content_type = message.content_type

    documents = getattr(message, content_type)
    if not isinstance(documents, list):
        documents = [documents]

    for doc in documents:
        file_id = doc.file_id
        file_info = bot.get_file(file_id)
        #print(file_info)
        file_url = 'https://api.telegram.org/file/bot{0}/{1}'.format(
            token, file_info.file_path)

        file_data = BytesIO(requests.get(file_url).content)
        try:
            file_name = doc.file_name
        except:
            file_name = file_info.file_path.replace("/", "_")
            send_photo_warning = True
        upload_io = convertapi.UploadIO(file_data, filename=file_name)
        converted_result = convertapi.convert('pdf', {'File': upload_io})
        converted_files = converted_result.save_files(tempfile.gettempdir())

        for file in converted_files:
            bot.send_document(message.chat.id, open(file, 'rb'))

    if send_photo_warning:
        bot.send_message(
            message.chat.id,
            'You sent this file as a photo. If you require better quality, please send it as a document.'
        )
def convertfile(request):
    if request.method == 'POST' and request.FILES['myfile']:
        myfile = request.FILES['myfile']
        fs = FileSystemStorage()
        filename = fs.save(myfile.name, myfile)
        uploaded_file_url = fs.url(filename)
        print(filename)
        upload_io = convertapi.UploadIO(open(f'media/{filename}', 'rb'))
        params = {'File': upload_io, 'converter': 'openoffice'}
        saved_files = convertapi.convert('pdf', params).save_files(
            tempfile.gettempdir())
        print("The PDF saved to %s" % saved_files)
        return HttpResponse("file Uploaded")
    return render(request, 'upload.html')
Beispiel #3
0
def do_convert(file_url, file_type, file_name, medium):
    convertapi.api_secret = settings.API_SECRET
    #base_url='https://pdftools.projecttopics.org'
    base_directory = settings.MEDIA_ROOT
    try:
        if file_type == 'docx':
            converter = Converter.objects.filter(
                convert_from=file_type.strip())[0]
        else:
            converter = Converter.objects.get(convert_from=file_type.strip())

    except Converter.DoesNotExist:
        return {'status': 404, 'data': 'Resource not found'}
    #
    # TODO: change file url before upload
    if medium == 'dropzone':
        file_url = '%s/%s' % (base_directory, file_url)
    try:
        upload_io = convertapi.UploadIO(open(file_url, 'rb'))
        result = convertapi.convert('pdf', {'File': upload_io})
        file_url = result.file.url
        return {'status': 200, 'data': file_url}
    except:
        return {'status': 400, 'data': 'API ERROR'}
def uploadFile(request):
    form = UploadFileForm
    if request.user.is_authenticated:
        user = request.user
    if request.method == "POST":
        myform = UploadFileForm(request.POST, request.FILES)
        if myform.is_valid():
            if "from" in request.GET:
                _from = request.GET['from']
                _to = request.GET['to']
                newFile = myform.save()
                filename = newFile.myfile
                path = file_converter(_from, _to, filename)
            else:
                newFile = myform.save()
                filename = newFile.myfile
                upload_io = convertapi.UploadIO(open(f'media/{filename}',
                                                     'rb'))
                params = {'File': upload_io, 'converter': 'openoffice'}
                c_file = convertapi.convert('pdf', params)
                path = c_file.save_files('media/')
            try:
                newData = convertedFile(c_file=path[0], user=user)
                newData.save()
                print(newData.pk)
            except Exception as e:
                print(f"Problem: {e}")
            return render(
                request, 'upload_file.html', {
                    'form': form,
                    'uid': user.id,
                    'uploaded_to': path[0],
                    'file_name': filename
                })
        return HttpResponse("something wrong")
    return render(request, 'upload_file.html', {'form': form, 'uid': user.id})
Beispiel #5
0
 def test_upload_io(self):
     bytes_io = io.BytesIO(b'test')
     upload_io = convertapi.UploadIO(bytes_io, 'test.txt')
     result = convertapi.convert('pdf', {'File': upload_io})
     assert result.conversion_cost > 0
Beispiel #6
0
def word_to_pdf(_in):
    convertapi.api_secret = os.environ['CONVERT_API_SECRET']
    upload_io = convertapi.UploadIO(open(_in, 'rb'))
    saved_files = convertapi.convert('pdf', { 'File': upload_io }).save_files(tempfile.gettempdir())
    print("The PDF saved to %s" % saved_files)
Beispiel #7
0
import convertapi
import os
import tempfile

convertapi.api_secret = os.environ['CONVERT_API_SECRET']  # your api secret

# Example of saving Word docx to PDF using OpenOffice converter
# https://www.convertapi.com/doc-to-pdf/openoffice

upload_io = convertapi.UploadIO(open('files/test.docx', 'rb'))
params = {'File': upload_io, 'converter': 'openoffice'}

saved_files = convertapi.convert('pdf',
                                 params).save_files(tempfile.gettempdir())

print("The PDF saved to %s" % saved_files)