Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Customizable PDF Metadata in ZugferdDocumentPdfMerger #208

Closed
MarkusJLechner opened this issue Dec 9, 2024 · 5 comments
Closed
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@MarkusJLechner
Copy link

Describe the feature

Allow setting custom metadata, specifically the title, when generating PDFs using ZugferdDocumentPdfMerger. The ability to customize other metadata fields (such as subject) should also be considered.

One approach could be to expose the pdfWriter instance so future customizations can be made without requiring new feature requests. Alternatively, make the hardcoded strings inside preparePdfMetadata customizable, ensuring flexibility.

Current Behavior

The current PDF metadata is hardcoded within preparePdfMetadata. Example:

private function preparePdfMetadata(): array
{
    $invoiceInformations = $this->extractInvoiceInformations();

    $dateString = date('Y-m-d', strtotime($invoiceInformations['date']));
    $title = sprintf('%s : %s %s', $invoiceInformations['seller'], $invoiceInformations['docTypeName'], $invoiceInformations['invoiceId']);
    $subject = sprintf('FacturX/ZUGFeRD %s %s dated %s issued by %s', $invoiceInformations['docTypeName'], $invoiceInformations['invoiceId'], $dateString, $invoiceInformations['seller']);

    $pdfMetadata = array(
        'author' => $invoiceInformations['seller'],
        'keywords' => sprintf('%s, FacturX/ZUGFeRD', $invoiceInformations['docTypeName']),
        'title' => $title,
        'subject' => $subject,
        'createdDate' => $invoiceInformations['date'],
        'modifiedDate' => date('Y-m-d\TH:i:s') . '+00:00',
    );

    return $pdfMetadata;
}

Expected Behavior

Allow setting a custom title like this:

return (new ZugferdDocumentPdfMerger($xml, $pdfContent))
  ->generateDocument()
  ->setTitle('Invoice 3') // Custom title
  ->downloadString('doc.pdf');

Alternatively, allow using a customizable format:

$title = sprintf('%1$s : %2$s %3$s', 
    $invoiceInformations['seller'], 
    $invoiceInformations['docTypeName'], 
    $invoiceInformations['invoiceId']);
$subject = sprintf('FacturX/ZUGFeRD %2$s %3$s dated %1$s issued by %4$s', 
    $dateString, 
    $invoiceInformations['docTypeName'], 
    $invoiceInformations['invoiceId'], 
    $invoiceInformations['seller']);
return (new ZugferdDocumentPdfMerger($xml, $pdfContent))
  ->generateDocument()
  ->setTitle('Rechnung : %2$s %3$s') // Customizable format
  ->downloadString('doc.pdf');

Context

Inside ZugferdDocumentPdfBuilderAbstract, the title and metadata are collected in preparePdfMetadata. The goal is to allow these values to be overridden without modifying core functionality.

Screenshots

Current implementation:
image

After attempting to add setTitle:
image

@MarkusJLechner MarkusJLechner added the enhancement New feature or request label Dec 9, 2024
@horstoeko horstoeko added this to the 01/2025 milestone Dec 9, 2024
@horstoeko
Copy link
Owner

Hi @MarkusJLechner,

I hope you have a nice day too. I have planned the feature for next year. Thank you for the detailed specification.

Kind regards

@jusefjames
Copy link

Hi @horstoeko,
maybe to add:
I already used FPDF a long time ago to create PDF files. As I am now just adding the lines needed to create the xml file, i already have a PDF "String" where I set the metadata like title, author, creator like that:

$pdf = new PDF_MC_Table(); //PDF_MC_Table extends FPDF 
$pdf->SetTitle('Title', true);
$pdf->SetAuthor('Author', true);
$pdf->SetCreator('Creator', true);

after doing this:

$pdfContent = $pdf->Output('', 'S');
$pdfBuilder = new ZugferdDocumentPdfBuilder($xmlContent, $pdfContent);
$pdfBuilder->generateDocument()->saveDocument($filename);

The document does no more contain any metadata. Not sure if this is related to this topic or if I do something wrong.

Thanks & Best regards

@horstoeko
Copy link
Owner

horstoeko commented Dec 11, 2024

Hi @MarkusJLechner,

I have created a PR #212

I've implemented the following:

  • Templating (setAuthorTemplate, setTitleTemplate, setSubjectTemplate)
  • A callback function to set the PDF metadata (author, subject, title)

The templates have four placeholders:

  • %1$s - Invoice No.
  • %2$s - Document Type
  • %3$s - Seller Name
  • %4$s - Invoice Date

The callback function takes the following parameters

  • $which - One of "title", "subject", "author"
  • $xmlContent - The content of the Invoice XML
  • $invoiceInformation - The content of the relevant XML-tags
  • $default - The default value (issued by the library)

The callback function has a higher priority than the templates.

I would ask you to review the pull request and give me feedback as soon as possible

Kind regards

@horstoeko horstoeko modified the milestones: 01/2025, 12/2024 Dec 11, 2024
horstoeko pushed a commit that referenced this issue Dec 11, 2024
…erEn16931Test::testCustomMetaInformation
horstoeko pushed a commit that referenced this issue Dec 11, 2024
horstoeko pushed a commit that referenced this issue Dec 11, 2024
@MarkusJLechner
Copy link
Author

looks good to me!

@horstoeko
Copy link
Owner

Hi @MarkusJLechner,

OK. Then I'll merge the PR and create a new release later today.

Thank you very much. And have a nice day.

horstoeko added a commit that referenced this issue Dec 11, 2024
#208 Customizable PDF Metadata -> Added Templates and Callback Function
Repository owner locked as resolved and limited conversation to collaborators Dec 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants