You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js中为phantom生成的PDF添加动态日期方形底部印章求助

Hey there, sorry to hear you’ve been stuck on this for 10 days—let’s break down some practical, actionable ways to add that dynamic date stamp (with a square border) to your existing PDF.

方案1:直接用PDF处理库修改已生成的PDF(推荐)

Since you already have the final PDF, using dedicated PDF manipulation libraries is a straightforward approach:

  • PyPDF2 + ReportLab (Python)
    This combo lets you generate the date stamp (square box + dynamic date) and overlay it onto your PDF in a few steps:

    1. Use ReportLab to create a temporary PDF with the square border and today’s date.
    2. Merge this stamp into your existing PDF using PyPDF2.

    Here’s a quick code example:

    from PyPDF2 import PdfWriter, PdfReader
    from reportlab.pdfgen import canvas
    from datetime import date
    import io
    
    # Generate the square date stamp
    packet = io.BytesIO()
    can = canvas.Canvas(packet)
    # Draw square border (x, y, width, height)
    can.rect(100, 100, 150, 50)
    # Add dynamic date
    can.drawString(130, 120, date.today().strftime("%Y-%m-%d"))
    can.save()
    
    # Merge stamp into original PDF
    packet.seek(0)
    stamp_pdf = PdfReader(packet)
    original_pdf = PdfReader(open("your_existing.pdf", "rb"))
    output = PdfWriter()
    
    # Add stamp to the first page (adjust index for other pages)
    target_page = original_pdf.pages[0]
    target_page.merge_page(stamp_pdf.pages[0])
    output.add_page(target_page)
    
    # Save the final PDF
    with open("pdf_with_stamp.pdf", "wb") as f:
        output.write(f)
    
  • iText (Java/.NET)
    If you work in a Java/.NET stack, iText provides robust APIs to draw shapes and text directly onto PDF pages. You can generate the dynamic date using your language’s built-in date utilities and overlay it with a square border easily.

方案2:Extend your PhantomJS HTML-to-PDF workflow

Since you originally used PhantomJS to generate the PDF, why not bake the date stamp into your HTML template upfront? This avoids post-processing entirely:
Add a fixed-position square element to your HTML, then use JavaScript to inject the dynamic date:

<!-- Place this anywhere in your HTML template -->
<div style="position: absolute; bottom: 50px; right: 50px; border: 1px solid #000; padding: 8px 16px; background: white;">
    <script>document.write(new Date().toISOString().split('T')[0])</script>
</div>

PhantomJS will render this dynamic content when generating the PDF, so your output will already include the date stamp.

方案3:Command-line tools (no coding required)

If you prefer no-code solutions, use these command-line tools together:

  1. ImageMagick to generate a square date stamp image:
    convert -size 150x50 xc:white -border 1 -bordercolor black -gravity center -pointsize 16 -annotate 0 "$(date +%Y-%m-%d)" stamp.png
    
  2. pdftk to overlay the image as a stamp on your PDF:
    pdftk your_existing.pdf stamp stamp.png output pdf_with_stamp.pdf
    

Content from stack exchange, question author rohit13807

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:32:46