Technical Article

PDF-verwerkingsprestaties optimaliseren: van minuten naar seconden

· PDF-programmeren

Praktische aandachtspunten om PDF-verwerking sneller en voorspelbaarder te maken in documenttoepassingen.

Dit artikel is bedoeld voor ontwikkelaars die met pdf-programmeren werken. Productnamen, API-namen, bestandsnamen en codefragmenten zijn bewust ongewijzigd gehouden, zodat de voorbeelden direct met de oorspronkelijke documentatie en broncode te vergelijken zijn.

Overzicht

De pagina beschrijft het probleemgebied, de relevante implementatiekeuzes en de controles die belangrijk zijn voordat de oplossing in een echte toepassing wordt gebruikt.

Codevoorbeeld

Het onderstaande codefragment is ongewijzigd uit de Engelse bron overgenomen om identifiers, API-aanroepen en syntaxis exact te behouden.

1
CopyPage.exe PDF-Reference-1.7-Fonts.pdf -page 1-3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Performance bottleneck: Complex tree reordering
procedure ReorderPagesByPagesTree(PDFDoc: TPDFDocument);
var
  i, j: Integer;
  TempList: TObjectList;
begin
  // This operation can be extremely slow for large documents
  for i := 0 to PDFDoc.PageCount - 1 do
  begin
    for j := 0 to PDFDoc.Objects.Count - 1 do
    begin
      // Nested loops create O(n²) complexity
      if IsPageObject(PDFDoc.Objects[j]) then
        ProcessPageTreeNode(PDFDoc.Objects[j]);
    end;
  end;
end;
1
2
3
4
5
6
7
8
9
// Unnecessary overhead: Processing all metadata
procedure ProcessDocumentMetadata(PDFDoc: TPDFDocument);
begin
  ExtractDocumentInfo(PDFDoc);        // Not needed for page copy
  ProcessBookmarks(PDFDoc);           // Not needed for page copy
  AnalyzeImageCompression(PDFDoc);    // Not needed for page copy
  ValidateDigitalSignatures(PDFDoc);  // Not needed for page copy
  OptimizeImageQuality(PDFDoc);       // Slow and unnecessary
end;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Optimized approach: Skip complex tree operations
function CopyPageOptimized(SourcePDF: TPDFDocument; PageIndex: Integer): TPDFDocument;
begin
  Result := TPDFDocument.Create;
  try
    // Skip complex tree analysis - go directly to page copying
    // This reduces processing time from minutes to seconds
    CopyPageDirectly(SourcePDF, PageIndex, Result);
    
    // Skip metadata copying for performance
    // Skip image optimization for performance
    // Skip bookmark processing for performance
    
  except
    on E: Exception do
    begin
      Result.Free;
      raise Exception.Create('Page copy failed: ' + E.Message);
    end;
  end;
end;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
procedure CopyPageDirectly(Source: TPDFDocument; PageIndex: Integer; Dest: TPDFDocument);
var
  SourcePage: TPDFPage;
  DestPage: TPDFPage;
begin
  // Get source page without tree traversal
  SourcePage := Source.GetPageDirect(PageIndex);
  if not Assigned(SourcePage) then
    raise Exception.Create('Source page not found');
  
  // Create destination page with minimal metadata
  DestPage := Dest.AddPage;
  DestPage.CopyContentFrom(SourcePage);
  
  // Skip unnecessary operations:
  // - Don't copy all document metadata
  // - Don't optimize images
  // - Don't process bookmarks
  // - Don't validate page tree structure
end;
1
2
3
4
// Example of unwanted temporary file creation in Release builds
// Temporary files created for verifying complex content stream processing
Creating temporary file: compressed_data_117.bin
Creating temporary file: compressed_data_200.bin<br>
1
2
3
4
5
6
7
8
9
10
// Remove functions that create temporary files
procedure OptimizeProcessing(PDFDoc: TPDFDocument);
begin
  // REMOVED: CreateDecompressedPDF(PDFDoc) - creates temporary files
  // REMOVED: GetCorrectPageOrderFromPagesTree(PDFDoc) - creates debug files
  // REMOVED: ReorderPageArrByPagesTree(PDFDoc) - creates backup files
  
  // Use direct memory processing instead
  ProcessPagesInMemory(PDFDoc);
end;
1
2
3
4
5
6
7
8
9
10
11
12
// Lazy loading approach for better performance
function GetPageContent(PDFDoc: TPDFDocument; PageIndex: Integer): string;
begin
  // Don't load entire document - just the required page
  if not IsPageLoaded(PageIndex) then
    LoadSinglePage(PDFDoc, PageIndex);
  
  Result := ExtractPageContentDirect(PDFDoc, PageIndex);
  
  // Clean up immediately after use
  UnloadPage(PageIndex);
end;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
type
  TProcessingOptions = record
    SkipMetadata: Boolean;
    SkipImageOptimization: Boolean;
    SkipBookmarks: Boolean;
    SkipPageTreeValidation: Boolean;
    UseSequentialMode: Boolean;
  end;
 
function CopyPageWithOptions(Source: TPDFDocument; PageIndex: Integer;
  Options: TProcessingOptions): TPDFDocument;
begin
  Result := TPDFDocument.Create;
  
  if Options.UseSequentialMode then
    SetSequentialProcessingMode(True);
  
  if Options.SkipPageTreeValidation then
    SkipComplexTreeOperations := True;
  
  // Perform only the required operations
  CopyPageMinimal(Source, PageIndex, Result);
end;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Implement comprehensive resource cleanup
procedure ProcessPDFWithCleanup(const FileName: string);
var
  PDFDoc: TPDFDocument;
  TempObjects: TObjectList;
begin
  PDFDoc := nil;
  TempObjects := TObjectList.Create(True);
  try
    PDFDoc := TPDFDocument.Create;
    PDFDoc.LoadFromFile(FileName);
    
    // Process document
    ProcessDocument(PDFDoc);
    
  finally
    // Ensure cleanup even if exceptions occur
    TempObjects.Free;
    if Assigned(PDFDoc) then
      PDFDoc.Free;
    
    // Force garbage collection
    System.GC;
  end;
end;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Memory pool for frequently used objects
type
  TPDFDocumentPool = class
  private
    FAvailableDocuments: TQueue;
    FMaxPoolSize: Integer;
  public
    function GetDocument: TPDFDocument;
    procedure ReturnDocument(Doc: TPDFDocument);
    constructor Create(MaxSize: Integer = 10);
  end;
 
function TPDFDocumentPool.GetDocument: TPDFDocument;
begin
  if FAvailableDocuments.Count > 0 then
  begin
    Result := FAvailableDocuments.Dequeue;
    Result.Reset; // Clear previous content
  end
  else
    Result := TPDFDocument.Create;
end;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Performance monitoring implementation
type
  TPerformanceProfiler = class
  private
    FStartTime: TDateTime;
    FOperationTimes: TDictionary<string, Double>;
  public
    procedure StartOperation(const OperationName: string);
    procedure EndOperation(const OperationName: string);
    procedure GenerateReport;
  end;
 
procedure TPerformanceProfiler.EndOperation(const OperationName: string);
var
  ElapsedTime: Double;
begin
  ElapsedTime := MilliSecondsBetween(Now, FStartTime);
  FOperationTimes.AddOrSetValue(OperationName, ElapsedTime);
  
  // Log slow operations
  if ElapsedTime > 1000 then // More than 1 second
    WriteLn(Format('WARNING: Slow operation %s took %.2f ms',
      [OperationName, ElapsedTime]));
end;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Monitor memory usage during processing
procedure MonitorMemoryUsage(const OperationName: string);
var
  MemStatus: TMemoryManagerState;
  UsedMemory: NativeUInt;
begin
  GetMemoryManagerState(MemStatus);
  UsedMemory := MemStatus.TotalAllocatedMediumBlockSize +
                MemStatus.TotalAllocatedLargeBlockSize;
  
  WriteLn(Format('%s: Memory usage: %d KB',
    [OperationName, UsedMemory div 1024]));
  
  // Alert on high memory usage
  if UsedMemory > 100 * 1024 * 1024 then // More than 100MB
    WriteLn('WARNING: High memory usage detected');
end;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Parallel processing implementation
procedure ProcessDocumentsParallel(const FileList: TStringList);
var
  ParallelTask: ITask;
  i: Integer;
begin
  // Create parallel tasks for document processing
  ParallelTask := TTask.Create(
    procedure
    var
      LocalIndex: Integer;
    begin
      TParallel.For(0, FileList.Count - 1,
        procedure(Index: Integer)
        begin
          ProcessSingleDocument(FileList[Index]);
        end);
    end);
  
  ParallelTask.Start;
  ParallelTask.Wait; // Wait for completion
end;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Thread-safe PDF processing
type
  TThreadSafePDFProcessor = class
  private
    FCriticalSection: TCriticalSection;
    FDocumentPool: TPDFDocumentPool;
  public
    function ProcessDocument(const FileName: string): Boolean;
    constructor Create;
    destructor Destroy; override;
  end;
 
function TThreadSafePDFProcessor.ProcessDocument(const FileName: string): Boolean;
var
  Doc: TPDFDocument;
begin
  FCriticalSection.Enter;
  try
    Doc := FDocumentPool.GetDocument;
  finally
    FCriticalSection.Leave;
  end;
  
  try
    // Process document outside critical section
    Doc.LoadFromFile(FileName);
    Result := ProcessDocumentContent(Doc);
  finally
    // Return document to pool
    FCriticalSection.Enter;
    try
      FDocumentPool.ReturnDocument(Doc);
    finally
      FCriticalSection.Leave;
    end;
  end;
end;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Quick validation to avoid expensive processing
function QuickValidatePDF(const FileName: string): Boolean;
var
  FileStream: TFileStream;
  Header: array[0..7] of AnsiChar;
begin
  Result := False;
  FileStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
  try
    // Quick header check - avoid loading entire file
    if FileStream.Size < 8 then Exit;
    
    FileStream.ReadBuffer(Header, 8);
    Result := CompareMem(@Header[0], @'%PDF-', 5);
    
    // Additional quick checks can be added here
    if not Result then
      WriteLn('Fast-fail: Invalid PDF header detected');
      
  finally
    FileStream.Free;
  end;
end;
1
2
3
4
5
6
7
8
9
10
11
Performance Test Results:
============================
Before Optimization:
- Single page copy: 120,150 ms (2 minutes)
- Memory usage: 85 MB
- Temporary files: 2 created
 
After Optimization:
- Single page copy: 1,230 ms (1.2 seconds)
- Memory usage: 12 MB
- Temporary files: 0 created
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Automated performance regression testing
procedure RunPerformanceRegressionTests;
var
  TestFiles: TStringList;
  i: Integer;
  StartTime, EndTime: TDateTime;
  ProcessingTime: Double;
begin
  TestFiles := GetTestFileList;
  try
    for i := 0 to TestFiles.Count - 1 do
    begin
      StartTime := Now;
      ProcessTestFile(TestFiles[i]);
      EndTime := Now;
      
      ProcessingTime := MilliSecondsBetween(EndTime, StartTime);
      
      // Alert if processing time exceeds baseline
      if ProcessingTime > GetBaselineTime(TestFiles[i]) * 1.2 then
        WriteLn(Format('REGRESSION: %s processing time increased to %.2f ms',
          [TestFiles[i], ProcessingTime]));
    end;
  finally
    TestFiles.Free;
  end;
end;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Prevent access violations with proper bounds checking
function SafeAccessPDFObject(PDFDoc: TPDFDocument; ObjectIndex: Integer): TPDFObject;
begin
  Result := nil;
  
  // Validate input parameters
  if not Assigned(PDFDoc) then
    Exit;
    
  if (ObjectIndex < 0) or (ObjectIndex >= PDFDoc.Objects.Count) then
    Exit;
  
  // Additional validation for object integrity
  try
    Result := PDFDoc.Objects[ObjectIndex];
    if not Assigned(Result) then
      Exit;
      
    // Verify object is properly initialized
    if Result.ObjectNumber <= 0 then
    begin
      Result := nil;
      Exit;
    end;
    
  except
    on E: Exception do
    begin
      // Log the error but don't crash
      WriteLn('WARNING: Object access failed: ' + E.Message);
      Result := nil;
    end;
  end;
end;
1
2
3
4
5
6
7
8
9
// Original problematic approach
Starting PDF processing...
Analyzing page tree structure... (31 seconds)
Reordering pages by tree hierarchy... (34 seconds)
Creating temporary decompressed file... (12 seconds)
Processing metadata and bookmarks... (17 seconds)
Optimizing image quality... (16 seconds)
Copying single page... (9 seconds)
Total time: 119 seconds (1.98 minutes)
1
2
3
4
5
6
7
8
// Optimized approach results
Starting PDF processing...
Direct page access (skipping tree analysis)... (0.2 seconds)
Copying page content directly... (0.8 seconds)
Skipping unnecessary metadata processing... (0 seconds)
Skipping image optimization... (0 seconds)
Cleanup and finalization... (0.2 seconds)
Total time: 1.2 seconds
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Don't optimize everything - focus on bottlenecks
procedure OptimizeBasedOnProfiling;
begin
  // Profile first to identify real bottlenecks
  StartProfiling;
  
  // Only optimize the operations that actually matter
  if IsBottleneck('PageTreeTraversal') then
    OptimizePageTreeTraversal;
    
  if IsBottleneck('MemoryAllocation') then
    ImplementMemoryPooling;
    
  // Don't waste time optimizing operations that take <1% of total time
  StopProfiling;
end;
1
2
3
4
5
6
7
8
9
10
// Implement basic functionality first
function ProcessPDFBasic(FileName: string): Boolean;
begin
  // Get basic functionality working correctly
  Result := LoadPDF(FileName) and ProcessContent and SaveResult;
  
  // Only add optimizations after confirming correctness
  if Result and NeedsOptimization then
    Result := ProcessPDFOptimized(FileName);
end;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Implement continuous performance monitoring
type
  TPerformanceMonitor = class
  private
    FMetrics: TDictionary<string, TPerformanceMetric>;
    FAlertThresholds: TDictionary<string, Double>;
  public
    procedure RecordOperation(Operation: string; Duration: Double; MemoryUsed: NativeUInt);
    procedure CheckForRegressions;
    procedure GeneratePerformanceReport;
  end;
 
procedure TPerformanceMonitor.CheckForRegressions;
var
  Operation: string;
  Metric: TPerformanceMetric;
  Threshold: Double;
begin
  for Operation in FMetrics.Keys do
  begin
    Metric := FMetrics[Operation];
    if FAlertThresholds.TryGetValue(Operation, Threshold) then
    begin
      if Metric.AverageDuration > Threshold then
        LogAlert(Format('Performance regression detected in %s: %.2f ms (threshold: %.2f ms)',
          [Operation, Metric.AverageDuration, Threshold]));
    end;
  end;
end;

Praktische aandachtspunten

  • Controleer invoerbestanden en foutpaden expliciet voordat u de routine in productie gebruikt.
  • Houd paginalay-out, lettertypen en coördinaten reproduceerbaar, vooral bij server-side verwerking.
  • Test het resultaat in meer dan één PDF-viewer wanneer rendering, annotaties of interactieve elementen belangrijk zijn.
  • Laat componentlevensduur en bestandshandles altijd via try/finally-achtige patronen opruimen.