-1

I need to convert a PDF File to TXT with javascript or php. Thise is my Originalfile: http://www.songmanage.com/du-bist-alles-c.pdf

and i want it to look in txt like:

Du bist alles
Tonart - C
Text und Musik: Simon Reger

Vers 1
C                      G                            a
1. Viele Wege gibt es hier, ich kann sie alle ausprobiern
                       F
doch nur einer führt zu dir, zum Leben.
C                     G                          a
Was hab ich noch zu verliern, ohne dich bin ich verlorn
                         F
hab das schon längst eingesehn

...

has someone a idea how to realise this?

Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
  • 1
    That is too broad a question for Stack Overflow. Here is some thought, though. A casual inspection shows the lyrics text is not actually *text*, they are all outlines; you must look to an OCR-based solution. – Jongware Apr 09 '18 at 08:39
  • 2
    Does this answer your question? [extract text from pdf in Javascript](https://stackoverflow.com/questions/1554280/extract-text-from-pdf-in-javascript) – John Goofy Jan 10 '21 at 13:07

1 Answers1

0

This text is only present in the PDF file as outlines, as the other user has commented above, an OCR solution is needed. If you're comfortable with Node.js here's some code that will do this. It depends on the http://www.ocrwebservice.com/ API, this will at least give you a free license code.

index.js

var rp = require('request-promise');
var fs = require('fs');

var args = process.argv.slice(2);

if (args.length < 3) {
    console.log('Please call like so: node index.js file username licenseCode');
    return -1;
}

var fileName = args[0];
var username = args[1];
var licenseCode = args[2];

var fileData = fs.readFileSync(fileName);

console.log('Performing OCR on: Filename: ' + fileName + " Length: " + fileData.length);

var authKey = 'Basic ' + (Buffer.from(username + ":" + licenseCode).toString('base64'));

var options = {
    method: 'POST',
    uri: 'http://www.ocrwebservice.com/restservices/processDocument?language=german&pagerange=1&outputformat=txt&gettext=true',
    body: fileData,
    headers: {
        'User-Agent': 'Request-Promise',
        'Authorization': authKey
    },
    json: false
};

rp(options)
    .then(function (parsedBody) {
        console.log('Success: ', parsedBody);
    })
    .catch(function (err) {
        console.error('Error: ', err);
    });

You would call the script like so: node index.js pdffile username licensecode.

This will give a response like so:

{
    "ErrorMessage": "",
    "OutputInformation": null,
    "AvailablePages": 22,
    "ProcessedPages": 1,
    "OCRText": [["Du bist alles Text and Musik: Simon Reger Vers 1 C G Am Viele Wege gibt es hier ich kann sie alle auspro - biern F Doch nur einer fiihrt zu dir zum Leben C G Am Was hab ich noch zu ver- liem ohne dich bin ich ver- lorn F Hab das schon !angst einge - sehn Am G F Darum lauf ich zu dir Chorus C G Du bist alles du bist alles fur mich Am F Du bist alles ich such dein Angesicht C G Du bist alles d
u bist alles fiir mich Am F Du bist alles ich liebe dich Vers 2 C G Am Halte dich nicht fest an dem was doch zer - bricht F Was wirklich wichtig ist sehn wir nur in seinem Licht C G Am Keinen Umweg mehr ich will nicht mehr im Kreis rou -tiern F Denn es gibt nur einen Weg Am G F Ich will mich ganz in dir verliern Tonart - C CCLI-Liednummer 5472893 © 2005 Reger, Simon Nutzung ausschlieglich im Rahmen der SongSelect-Nutzungsbedingungen. Alle Rechte vorbehalten. www.ccli.de CCLI-Lizenznummer 1293482
 "]],
    "OutputFileUrl": "",
    "OutputFileUrl2": "",
    "OutputFileUrl3": "",
    "Reserved": [],
    "OCRWords": [],
    "TaskDescription": null
} 

The output will also contain a link to a text file (or PDF or DOC) containing your text, this might be more useful for you.

Terry Lennox
  • 29,471
  • 5
  • 28
  • 40