How to generate an SQL file with DBMS_DATAPUMP
Ok, I didn't realise that creating such an export file consists of two steps. First, an ordinary dump file must be created, then this dump file is used to create an sql file.

Since I didn't have such a dump file, the script as posted would not work.

Here are the two steps, so that an sql file can be created

declare

  datapump_job number;
  job_state    varchar2(20);

begin

  datapump_job := dbms_datapump.open(
    operation    =&gt; 'EXPORT',
    job_mode     =&gt; 'SCHEMA',
    remote_link  =&gt;  null,
    job_name     =&gt; 'Export dump file',
    version      =&gt; 'LATEST'
  );

  dbms_output.put_line('datapump_job: ' || datapump_job);


  dbms_datapump.add_file(
    handle    =&gt;  datapump_job,
    filename  =&gt; 'export.dmp',
    directory =&gt; 'DATAPUMP_DIR',
    filetype  =&gt;  dbms_datapump.ku$_file_type_dump_file);


  dbms_datapump.start_job(
    handle       =&gt; datapump_job,
    skip_current =&gt; 0,
    abort_step   =&gt; 0);

  dbms_datapump.wait_for_job(datapump_job, job_state);

  dbms_output.put_line('Job state: ' || job_state);

  dbms_datapump.detach(datapump_job);

end;
/


and the script that creates the sql file:

declare

  datapump_job number;
  job_state    varchar2(20);

begin

  datapump_job := dbms_datapump.open(
    operation    =&gt; 'SQL_FILE',
    job_mode     =&gt; 'SCHEMA',
    remote_link  =&gt;  null,
    job_name     =&gt; 'Export SQL file',
    version      =&gt; 'LATEST'
  );

  dbms_output.put_line('datapump_job: ' || datapump_job);

  dbms_datapump.add_file(
    handle    =&gt;  datapump_job,
    filename  =&gt; 'export.dmp',
    directory =&gt; 'DATAPUMP_DIR',
    filetype  =&gt;  dbms_datapump.ku$_file_type_dump_file);

  dbms_datapump.add_file(
    handle    =&gt;  datapump_job,
    filename  =&gt; 'schema.sql',
    directory =&gt; 'DATAPUMP_DIR',
    filetype  =&gt;  dbms_datapump.ku$_file_type_sql_file);

  dbms_datapump.start_job(
    handle       =&gt; datapump_job,
    skip_current =&gt; 0,
    abort_step   =&gt; 0);

  dbms_datapump.wait_for_job(datapump_job, job_state);

  dbms_output.put_line('Job state: ' || job_state);

  dbms_datapump.detach(datapump_job);

end;