0

I'm building a PDF on my FTP Folder to send it via a Daemon, the file it's OK, but every time I put it there again this file is appended with information, PDF naturally only reads the LAST instruction, but in order to create a clean File I need to create each time a blank PDF.

  1. Create PDF
  2. Write info (UTL_FILE.PUT_LINE)
  3. Close File

but if I need to create another file with same layer but new info, my process to create a file with some name and this find the old one and APPEND the information.

I need to blank that file and put the new info, what should I do to avoid append that info?

CODE: (it's the library of PL_FPDF modified to write files in a directory)

--some code!
elsif (myDest = 'F') then
    -- DFVD -- Enviar a directorio
    -- Content Restriction
    v_len := 1;

    for i in pdfDoc.first..pdfDoc.last loop
        v_clob := to_clob(pdfDoc(i));
        if v_clob is not null then
            v_in := 1;
            v_out := 1;
            v_lang := 0;
            v_warning := 0;
            v_len := dbms_lob.getlength(v_clob);

            dbms_lob.convertToBlob(v_blob, v_clob, v_len, v_in, v_out, dbms_lob.default_csid, v_lang, v_warning);


            dbms_lob.append(v_doc, dbms_lob.substr(v_blob, v_len));   
        end if;
    end loop;

    l_blob_len := DBMS_LOB.getlength(v_doc);
    l_file := UTL_FILE.fopen('FTP_DIR',myName,'w', 32767);

    WHILE l_pos < l_blob_len LOOP
        DBMS_LOB.read(v_doc, l_amount, l_pos, l_buffer);
        UTL_FILE.put_raw(l_file, l_buffer, TRUE);
        l_pos := l_pos + l_amount;
    END LOOP;

    UTL_FILE.fclose(l_file);
else
--some code!

THE INTERNAL CODE OF THE PDF LOOKS LIKE THIS:

%PDF-1.3
3 0 obj
<</Type /Page
/Parent 1 0 R
/Resources 2 0 R
/Contents 4 0 R>>
endobj
4 0 obj
<</Length 2401>>
stream
2 J
0.56 w
BT /F1 12.0 Tf ET
BT /F2 20.0 Tf ET
0.0 0.4 0.8 rg
28.35 813.54 538.57 -28.34 re f 28.35 813.54 m 28.35 785.19 l S 28.35 813.54 m 566.92 813.54 l S 566.92 813.54 m 566.92 785.19 l S 28.35 785.19 m 566.92 785.19 l S q 1.0 1.0 1.0 rg BT 190.95 793.36 Td (Informe Liquidaciones) Tj ET Q
BT /F2 9.0 Tf ET
q 0.0 g BT 31.18 768.32 Td (Fecha: 17-02-2016) Tj ET Q
28.35 728.50 m 28.35 700.15 l S 28.35 728.50 m 170.08 728.50 l S 28.35 700.15 m 170.08 700.15 l S q 0.0 g BT 115.74 711.62 Td (Devolucion:) Tj ET Q
170.08 728.50 m 566.92 728.50 l S 566.92 728.50 m 566.92 700.15 l S 170.08 700.15 m 566.92 700.15 l S 
28.35 697.31 m 28.35 668.97 l S 28.35 697.31 m 396.85 697.31 l S q 0.0 g BT 304.99 680.44 Td (Cantidad de Primas  ) Tj ET Q
396.85 697.31 m 566.92 697.31 l S 566.92 697.31 m 566.92 668.97 l S q 0.0 g BT 399.68 680.44 Td (  Monto Recaudado) Tj ET Q
28.35 668.97 m 28.35 640.62 l S q 0.0 g BT 31.18 652.09 Td (  Recaudado Tarjeta Ripley) Tj ET Q
q 0.0 g BT 389.01 652.09 Td (  ) Tj ET Q
566.92 668.97 m 566.92 640.62 l S q 0.0 g BT 399.68 652.09 Td (  ) Tj ET Q
28.35 640.62 m 28.35 612.28 l S q 0.0 g BT 31.18 623.75 Td (  Recaudado OMP) Tj ET Q
q 0.0 g BT 389.01 623.75 Td (  ) Tj ET Q
566.92 640.62 m 566.92 612.28 l S q 0.0 g BT 399.68 623.75 Td (  ) Tj ET Q
28.35 612.28 m 28.35 583.93 l S 28.35 583.93 m 396.85 583.93 l S q 0.0 g BT 31.18 595.40 Td (  Devoluciones no Pago Mes Anterior:) Tj ET Q
566.92 612.28 m 566.92 583.93 l S 396.85 583.93 m 566.92 583.93 l S q 0.0 g BT 399.68 595.40 Td (  ) Tj ET Q
28.35 578.26 m 28.35 549.91 l S 28.35 578.26 m 396.85 578.26 l S 28.35 549.91 m 396.85 549.91 l S q 0.0 g BT 313.50 561.39 Td (Total Recaudado:  ) Tj ET Q
396.85 578.26 m 566.92 578.26 l S 566.92 578.26 m 566.92 549.91 l S 396.85 549.91 m 566.92 549.91 l S q 0.0 g BT 399.68 561.39 Td (  ) Tj ET Q
BT /F1 8.0 Tf ET
q 0.0 g BT 31.18 526.25 Td (Comentarios) Tj ET Q
BT /F1 9.0 Tf ET
28.35 521.57 m 28.35 408.18 l S 28.35 521.57 m 566.92 521.57 l S 566.92 521.57 m 566.92 408.18 l S 28.35 408.18 m 566.92 408.18 l S 
BT /F2 9.0 Tf ET
q 0.0 g BT 67.93 199.97 Td (_____________________) Tj ET Q
q 0.0 g BT 252.18 199.97 Td (_____________________) Tj ET Q
q 0.0 g BT 429.34 199.97 Td (_____________________) Tj ET Q
q 0.0 g BT 86.46 185.80 Td (some guy) Tj ET Q
q 0.0 g BT 260.96 185.80 Td (some guy) Tj ET Q
q 0.0 g BT 447.62 185.80 Td (some guy) Tj ET Q

endstream
endobj
1 0 obj
<</Type /Pages
/Kids [3 0 R ]
/Count 1
/MediaBox [0 0 595.28 841.89]
>>
endobj
5 0 obj
<</Type /Font
/BaseFont /Helvetica
/Subtype /Type1
/Encoding /WinAnsiEncoding
>>
endobj
6 0 obj
<</Type /Font
/BaseFont /Helvetica-Bold
/Subtype /Type1
/Encoding /WinAnsiEncoding
>>
endobj
2 0 obj
<<
/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]
/Font <<
/F1 5 0 R
/F2 6 0 R
>>
/XObject <<
>>
>>
endobj
7 0 obj
<<
/Producer (PL_FPDF 0.9.3 portage pour Laclasse.com par P.G. Levallois de la version 1.53 de PHP/FPDF d'Olivier Plathey.)
/Title (Informe Liquidaciones a Companias)
/Author ()
/Creator ()
/CreationDate (D:20160217160717)
>>
endobj
8 0 obj
<<
/Type /Catalog
/Pages 1 0 R
/OpenAction [3 0 R /FitH null]
/PageLayout /OneColumn
>>
endobj
xref
0 9
0000000000 65535 f 
0000002538 00000 n 
0000002822 00000 n 
0000000009 00000 n 
0000000087 00000 n 
0000002625 00000 n 
0000002721 00000 n 
0000002936 00000 n 
0000003189 00000 n 
trailer
<<
/Size 9
/Root 8 0 R
/Info 7 0 R
>>
startxref
3292
%%EOF
%PDF-1.3
3 0 obj
<</Type /Page
/Parent 1 0 R
/Resources 2 0 R
/Contents 4 0 R>>
endobj
4 0 obj
<</Length 2401>>
stream
2 J
0.56 w
BT /F1 12.0 Tf ET
BT /F2 20.0 Tf ET
0.0 0.4 0.8 rg
28.35 813.54 538.57 -28.34 re f 28.35 813.54 m 28.35 785.19 l S 28.35 813.54 m 566.92 813.54 l S 566.92 813.54 m 566.92 785.19 l S 28.35 785.19 m 566.92 785.19 l S q 1.0 1.0 1.0 rg BT 190.95 793.36 Td (Informe Liquidaciones) Tj ET Q
BT /F2 9.0 Tf ET
q 0.0 g BT 31.18 768.32 Td (Fecha: 17-02-2016) Tj ET Q
28.35 728.50 m 28.35 700.15 l S 28.35 728.50 m 170.08 728.50 l S 28.35 700.15 m 170.08 700.15 l S q 0.0 g BT 115.74 711.62 Td (Devolucion:) Tj ET Q
170.08 728.50 m 566.92 728.50 l S 566.92 728.50 m 566.92 700.15 l S 170.08 700.15 m 566.92 700.15 l S 
28.35 697.31 m 28.35 668.97 l S 28.35 697.31 m 396.85 697.31 l S q 0.0 g BT 304.99 680.44 Td (Cantidad de Primas  ) Tj ET Q
396.85 697.31 m 566.92 697.31 l S 566.92 697.31 m 566.92 668.97 l S q 0.0 g BT 399.68 680.44 Td (  Monto Recaudado) Tj ET Q
28.35 668.97 m 28.35 640.62 l S q 0.0 g BT 31.18 652.09 Td (  Recaudado Tarjeta Ripley) Tj ET Q
q 0.0 g BT 389.01 652.09 Td (  ) Tj ET Q
566.92 668.97 m 566.92 640.62 l S q 0.0 g BT 399.68 652.09 Td (  ) Tj ET Q
28.35 640.62 m 28.35 612.28 l S q 0.0 g BT 31.18 623.75 Td (  Recaudado OMP) Tj ET Q
q 0.0 g BT 389.01 623.75 Td (  ) Tj ET Q
566.92 640.62 m 566.92 612.28 l S q 0.0 g BT 399.68 623.75 Td (  ) Tj ET Q
28.35 612.28 m 28.35 583.93 l S 28.35 583.93 m 396.85 583.93 l S q 0.0 g BT 31.18 595.40 Td (  Devoluciones no Pago Mes Anterior:) Tj ET Q
566.92 612.28 m 566.92 583.93 l S 396.85 583.93 m 566.92 583.93 l S q 0.0 g BT 399.68 595.40 Td (  ) Tj ET Q
28.35 578.26 m 28.35 549.91 l S 28.35 578.26 m 396.85 578.26 l S 28.35 549.91 m 396.85 549.91 l S q 0.0 g BT 313.50 561.39 Td (Total Recaudado:  ) Tj ET Q
396.85 578.26 m 566.92 578.26 l S 566.92 578.26 m 566.92 549.91 l S 396.85 549.91 m 566.92 549.91 l S q 0.0 g BT 399.68 561.39 Td (  ) Tj ET Q
BT /F1 8.0 Tf ET
q 0.0 g BT 31.18 526.25 Td (Comentarios) Tj ET Q
BT /F1 9.0 Tf ET
28.35 521.57 m 28.35 408.18 l S 28.35 521.57 m 566.92 521.57 l S 566.92 521.57 m 566.92 408.18 l S 28.35 408.18 m 566.92 408.18 l S 
BT /F2 9.0 Tf ET
q 0.0 g BT 67.93 199.97 Td (_____________________) Tj ET Q
q 0.0 g BT 252.18 199.97 Td (_____________________) Tj ET Q
q 0.0 g BT 429.34 199.97 Td (_____________________) Tj ET Q
q 0.0 g BT 86.46 185.80 Td (some guy) Tj ET Q
q 0.0 g BT 260.96 185.80 Td (some guy) Tj ET Q
q 0.0 g BT 447.62 185.80 Td (some guy) Tj ET Q

endstream
endobj
1 0 obj
<</Type /Pages
/Kids [3 0 R ]
/Count 1
/MediaBox [0 0 595.28 841.89]
>>
endobj
5 0 obj
<</Type /Font
/BaseFont /Helvetica
/Subtype /Type1
/Encoding /WinAnsiEncoding
>>
endobj
6 0 obj
<</Type /Font
/BaseFont /Helvetica-Bold
/Subtype /Type1
/Encoding /WinAnsiEncoding
>>
endobj
2 0 obj
<<
/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]
/Font <<
/F1 5 0 R
/F2 6 0 R
>>
/XObject <<
>>
>>
endobj
7 0 obj
<<
/Producer (PL_FPDF 0.9.3 portage pour Laclasse.com par P.G. Levallois de la version 1.53 de PHP/FPDF d'Olivier Plathey.)
/Title (Informe Liquidaciones a Companias)
/Author ()
/Creator ()
/CreationDate (D:20160217160747)
>>
endobj
8 0 obj
<<
/Type /Catalog
/Pages 1 0 R
/OpenAction [3 0 R /FitH null]
/PageLayout /OneColumn
>>
endobj
xref
0 9
0000000000 65535 f 
0000006086 00000 n 
0000006370 00000 n 
0000003557 00000 n 
0000003635 00000 n 
0000006173 00000 n 
0000006269 00000 n 
0000006484 00000 n 
0000006737 00000 n 
trailer
<<
/Size 9
/Root 8 0 R
/Info 7 0 R
>>
startxref
6840
%%EOF
%PDF-1.3
3 0 obj
<</Type /Page
/Parent 1 0 R
/Resources 2 0 R
/Contents 4 0 R>>
endobj
4 0 obj
<</Length 2401>>
stream
2 J
0.56 w
BT /F1 12.0 Tf ET
BT /F2 20.0 Tf ET
0.0 0.4 0.8 rg
28.35 813.54 538.57 -28.34 re f 28.35 813.54 m 28.35 785.19 l S 28.35 813.54 m 566.92 813.54 l S 566.92 813.54 m 566.92 785.19 l S 28.35 785.19 m 566.92 785.19 l S q 1.0 1.0 1.0 rg BT 190.95 793.36 Td (Informe Liquidaciones) Tj ET Q
BT /F2 9.0 Tf ET
q 0.0 g BT 31.18 768.32 Td (Fecha: 17-02-2016) Tj ET Q
28.35 728.50 m 28.35 700.15 l S 28.35 728.50 m 170.08 728.50 l S 28.35 700.15 m 170.08 700.15 l S q 0.0 g BT 115.74 711.62 Td (Devolucion:) Tj ET Q
170.08 728.50 m 566.92 728.50 l S 566.92 728.50 m 566.92 700.15 l S 170.08 700.15 m 566.92 700.15 l S 
28.35 697.31 m 28.35 668.97 l S 28.35 697.31 m 396.85 697.31 l S q 0.0 g BT 304.99 680.44 Td (Cantidad de Primas  ) Tj ET Q
396.85 697.31 m 566.92 697.31 l S 566.92 697.31 m 566.92 668.97 l S q 0.0 g BT 399.68 680.44 Td (  Monto Recaudado) Tj ET Q
28.35 668.97 m 28.35 640.62 l S q 0.0 g BT 31.18 652.09 Td (  Recaudado Tarjeta Ripley) Tj ET Q
q 0.0 g BT 389.01 652.09 Td (  ) Tj ET Q
566.92 668.97 m 566.92 640.62 l S q 0.0 g BT 399.68 652.09 Td (  ) Tj ET Q
28.35 640.62 m 28.35 612.28 l S q 0.0 g BT 31.18 623.75 Td (  Recaudado OMP) Tj ET Q
q 0.0 g BT 389.01 623.75 Td (  ) Tj ET Q
566.92 640.62 m 566.92 612.28 l S q 0.0 g BT 399.68 623.75 Td (  ) Tj ET Q
28.35 612.28 m 28.35 583.93 l S 28.35 583.93 m 396.85 583.93 l S q 0.0 g BT 31.18 595.40 Td (  Devoluciones no Pago Mes Anterior:) Tj ET Q
566.92 612.28 m 566.92 583.93 l S 396.85 583.93 m 566.92 583.93 l S q 0.0 g BT 399.68 595.40 Td (  ) Tj ET Q
28.35 578.26 m 28.35 549.91 l S 28.35 578.26 m 396.85 578.26 l S 28.35 549.91 m 396.85 549.91 l S q 0.0 g BT 313.50 561.39 Td (Total Recaudado:  ) Tj ET Q
396.85 578.26 m 566.92 578.26 l S 566.92 578.26 m 566.92 549.91 l S 396.85 549.91 m 566.92 549.91 l S q 0.0 g BT 399.68 561.39 Td (  ) Tj ET Q
BT /F1 8.0 Tf ET
q 0.0 g BT 31.18 526.25 Td (Comentarios) Tj ET Q
BT /F1 9.0 Tf ET
28.35 521.57 m 28.35 408.18 l S 28.35 521.57 m 566.92 521.57 l S 566.92 521.57 m 566.92 408.18 l S 28.35 408.18 m 566.92 408.18 l S 
BT /F2 9.0 Tf ET
q 0.0 g BT 67.93 199.97 Td (_____________________) Tj ET Q
q 0.0 g BT 252.18 199.97 Td (_____________________) Tj ET Q
q 0.0 g BT 429.34 199.97 Td (_____________________) Tj ET Q
q 0.0 g BT 86.46 185.80 Td (some guy) Tj ET Q
q 0.0 g BT 260.96 185.80 Td (some guy) Tj ET Q
q 0.0 g BT 447.62 185.80 Td (some guy) Tj ET Q

endstream
endobj
1 0 obj
<</Type /Pages
/Kids [3 0 R ]
/Count 1
/MediaBox [0 0 595.28 841.89]
>>
endobj
5 0 obj
<</Type /Font
/BaseFont /Helvetica
/Subtype /Type1
/Encoding /WinAnsiEncoding
>>
endobj
6 0 obj
<</Type /Font
/BaseFont /Helvetica-Bold
/Subtype /Type1
/Encoding /WinAnsiEncoding
>>
endobj
2 0 obj
<<
/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]
/Font <<
/F1 5 0 R
/F2 6 0 R
>>
/XObject <<
>>
>>
endobj
7 0 obj
<<
/Producer (PL_FPDF 0.9.3 portage pour Laclasse.com par P.G. Levallois de la version 1.53 de PHP/FPDF d'Olivier Plathey.)
/Title (Informe Liquidaciones a Companias)
/Author ()
/Creator ()
/CreationDate (D:20160217160748)
>>
endobj
8 0 obj
<<
/Type /Catalog
/Pages 1 0 R
/OpenAction [3 0 R /FitH null]
/PageLayout /OneColumn
>>
endobj
xref
0 9
0000000000 65535 f 
0000009634 00000 n 
0000009918 00000 n 
0000007105 00000 n 
0000007183 00000 n 
0000009721 00000 n 
0000009817 00000 n 
0000010032 00000 n 
0000010285 00000 n 
trailer
<<
/Size 9
/Root 8 0 R
/Info 7 0 R
>>
startxref
10388
%%EOF

But the file is ok:

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    You're appending all the elements of your pdfDoc collection into one v_doc BLOB, and then writing that out to a single file. Is that what you want? That file will be overwritten each time you run this, but only once per run. Are you saying the file is growing each time you run this? – Alex Poole Feb 17 '16 at 16:42
  • yes, the file is growing twice everytime is writed (NOTE: what is the point in downvote something like this) – Diego Fernando Villarroel Diaz Feb 17 '16 at 19:06
  • The only way I can see that happening, since you're opening it with 'w', is if v_doc is stateful and never reset; or you're reading the existing file into pdfDoc which seems unlikely too. Something odd is going on that we can't see, I think. – Alex Poole Feb 17 '16 at 19:09
  • so, should i "delete" this file and open a new UTL_FILE "W", how can i check if the file exists? – Diego Fernando Villarroel Diaz Feb 17 '16 at 19:17
  • You're already opening with 'w', not 'a', so it should be overwritten. What happens if you manually remove the file between runs - does it get recreated as the correct size? – Alex Poole Feb 17 '16 at 19:22
  • yes, the file get the correct size then... first execute: 4kb second execute: 8kb third execute: 12k the file is appended everytime i run the script and this is the part where i put all the new information and close the File, – Diego Fernando Villarroel Diaz Feb 17 '16 at 19:31
  • Then it sounds like your v_doc isn't being reset, but that implies it's a stateful package-level variable, which still sounds unlikely. Sorry, not sure what to suggest if that isn't the case. – Alex Poole Feb 17 '16 at 19:36

1 Answers1

2

After a short research I couldn't find anything, so I create a "patch" solution may work in this case:

--some code!
elsif (myDest = 'F') then
    -- DFVD -- Enviar a directorio
    -- Content Restriction
    v_len := 1;

    for i in pdfDoc.first..pdfDoc.last loop
        v_clob := to_clob(pdfDoc(i));
        if v_clob is not null then
            v_in := 1;
            v_out := 1;
            v_lang := 0;
            v_warning := 0;
            v_len := dbms_lob.getlength(v_clob);

            dbms_lob.convertToBlob(v_blob, v_clob, v_len, v_in, v_out, dbms_lob.default_csid, v_lang, v_warning);


            dbms_lob.append(v_doc, dbms_lob.substr(v_blob, v_len));   
        end if;
    end loop;

    //CHECK IF FILE EXISTS
    begin
        l_file := UTL_FILE.fopen('FTP_DIR',myName,'r');
        if UTL_FILE.is_open(l_file) then
            UTL_FILE.fclose(l_file);
        end if;     
    exception
        when others then
            NULL;
    end; 
    //CHECK IF FILE EXISTS

    l_blob_len := DBMS_LOB.getlength(v_doc);
    l_file := UTL_FILE.fopen('FTP_DIR',myName,'w', 32767);

    WHILE l_pos < l_blob_len LOOP
        DBMS_LOB.read(v_doc, l_amount, l_pos, l_buffer);
        UTL_FILE.put_raw(l_file, l_buffer, TRUE);
        l_pos := l_pos + l_amount;
    END LOOP;

    UTL_FILE.fclose(l_file);
else
--some code!

now the procedure Check if the File exists and if exists "fremove" the file in order to ensure to create a new one.

halfer
  • 19,824
  • 17
  • 99
  • 186