Jump to content

combine pdf files with UniSession.SendFile


Muhammad Idris

Recommended Posts

how to combine pdf files into one file with UniSession.SendFile ?

The code I made can only download multiple files, it can't combine into one PDF file :

  QBiaya.Close;
  QBiaya.SQL.Clear;
  QBiaya.SQL.Add('SELECT ROW_NUMBER() OVER(ORDER BY A.Id_Hasil ASC) AS Nmr,A.*,C.NAMALENGKAP '+
    'FROM ASESMEN_HASIL_PENUNJANG_LAINNYA_PDF AS A '+
    'INNER JOIN TUSER AS C ON A.NAMAUSER=C.NAMAUSER '+
    'WHERE A.NO_REG = '+QuotedStr(noreg)+' '+
    'ORDER BY A.Id_Hasil');
  QBiaya.Open;
  QBiaya.First;
  while Not QBiaya.Eof do
  begin
    CopyFile(pchar(UniMainModule.folderPath+QBiayaNama_File.AsString),pchar(UniServerModule.LocalCachePath+'\'+QBiayaNama_File.AsString),false);
    pathfile:=trim( Copy(UniServerModule.LocalCachePath,AnsiPos('cache',UniServerModule.LocalCachePath),length(UniServerModule.LocalCachePath)));
    UniSession.SendFile(UniServerModule.LocalCachePath+'\'+QBiayaNama_File.AsString);
    QBiaya.Next;
  end;

Link to comment
Share on other sites

1 hora atrás, Muhammad Idris disse:

Então, existe uma solução para que vários arquivos PDF possam se tornar um arquivo PDF?

I have a routine here that can generate thousands of PDFs,

and the solution to downloading them was simple.

Just get all pdf and zip it. Download the zip and be happy.

Link to comment
Share on other sites

I build my PDF files at runtime, firstly as HTML Files, then finally converting to PDF so I believe it would be relatively easy to CONCATINATE multiple HTML files before conversion into one PDF.

Link to comment
Share on other sites

On 2/9/2024 at 10:54 PM, Fred Montier said:

Existem dezenas de bibliotecas pdf vcl que você pode fazer isso. Até mesmo JS libs.

PDFium é apenas um deles e é gratuito.

Não está relacionado à uniGUI.

Does PdfiumLib convert html to pdf?

Link to comment
Share on other sites

  • 3 weeks later...
On 3/2/2024 at 12:39 AM, Muhammad Idris said:

How to run it in Unigui?

install pdftk in your environment


and you can use this code:
 

procedure MergeAndEncryptPDFs(const AInputFiles: string; const AOutputFile: string; const AUserPassword: string);
var
  CommandLine: string;
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
begin
  CommandLine := 'pdftk ' + AInputFiles + ' cat output ' + AOutputFile + ' user_pw ' + AUserPassword;

  FillChar(StartupInfo, SizeOf(StartupInfo), 0);
  StartupInfo.cb := SizeOf(StartupInfo);

  if CreateProcess(nil, PChar(CommandLine), nil, nil, False, 0, nil, nil, StartupInfo, ProcessInfo) then
  begin
    WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
    CloseHandle(ProcessInfo.hProcess);
    CloseHandle(ProcessInfo.hThread);
	end
	else
		raise Exception.Create('not created');
end;


to call it:

 

procedure TMainForm.UniButton1Click(Sender: TObject);
var 
	OutPdf: string;
begin
	OutPdf :=  UniServerModule.NewCacheFile('pdf', fileName);
	MergeAndEncryptPDFs('example1.pdf,example2.pdf', OutPdf, '');
end;


you can modify the code to omit the password protection for your pdf.

hope it helps

 

Link to comment
Share on other sites

  • 2 weeks later...
On 3/3/2024 at 10:34 PM, Kurt said:

install pdftk in your environment


and you can use this code:
 

procedure MergeAndEncryptPDFs(const AInputFiles: string; const AOutputFile: string; const AUserPassword: string);
var
  CommandLine: string;
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
begin
  CommandLine := 'pdftk ' + AInputFiles + ' cat output ' + AOutputFile + ' user_pw ' + AUserPassword;

  FillChar(StartupInfo, SizeOf(StartupInfo), 0);
  StartupInfo.cb := SizeOf(StartupInfo);

  if CreateProcess(nil, PChar(CommandLine), nil, nil, False, 0, nil, nil, StartupInfo, ProcessInfo) then
  begin
    WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
    CloseHandle(ProcessInfo.hProcess);
    CloseHandle(ProcessInfo.hThread);
	end
	else
		raise Exception.Create('not created');
end;


to call it:

 

procedure TMainForm.UniButton1Click(Sender: TObject);
var 
	OutPdf: string;
begin
	OutPdf :=  UniServerModule.NewCacheFile('pdf', fileName);
	MergeAndEncryptPDFs('example1.pdf,example2.pdf', OutPdf, '');
end;


you can modify the code to omit the password protection for your pdf.

hope it helps

 

image.png.2c88f733bc4a96615c817d9f000c1b60.png

 

Thank you sir, I tried but couldn't create a PDF and how do I get the PDF file to be taken from a loop like this?

 

    QBiaya.Close;
    QBiaya.SQL.Clear;
    QBiaya.SQL.Add('SELECT ROW_NUMBER() OVER(ORDER BY A.Id_Hasil ASC) AS Nmr,A.*,C.NAMALENGKAP '+
      'FROM ASESMEN_HASIL_PENUNJANG_LAINNYA_PDF AS A '+
      'INNER JOIN TUSER AS C ON A.NAMAUSER=C.NAMAUSER '+
      'WHERE A.NO_REG = '+QuotedStr(noreg)+' '+
      'ORDER BY A.Id_Hasil');
    QBiaya.Open;
    QBiaya.First;
    while Not QBiaya.Eof do
    begin
      CopyFile(pchar(UniMainModule.folderPath+QBiayaNama_File.AsString),pchar(UniServerModule.LocalCachePath+'\'+QBiayaNama_File.AsString),false);
      pathfile:=trim( Copy(UniServerModule.LocalCachePath,AnsiPos('cache',UniServerModule.LocalCachePath),length(UniServerModule.LocalCachePath)));
      UniSession.SendFile(UniServerModule.LocalCachePath+'\'+QBiayaNama_File.AsString);
      QBiaya.Next;
    end;

    for Path in TDirectory.GetFiles(UniServerModule.LocalCachePath+'\')  do
    begin
      UniListBox1.Items.Add(ExtractFileName( Path ));
    end;

    OutPdf :=  UniServerModule.NewCacheFile('pdf', fileName);
    MergeAndEncryptPDFs('EKG MADAA.pdf,KULTUR MADAA.pdf', OutPdf, '');

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...