ajaxray / markpdf Goto Github PK
View Code? Open in Web Editor NEWWatermark PDF files using image or text
License: GNU Affero General Public License v3.0
Watermark PDF files using image or text
License: GNU Affero General Public License v3.0
I am trying to add 2 lines(rows or line break) placeholder. I am using php to call command and trying to add new line "\n".
It gives no error and also no placeholder added in pdf. If I remove new line chars it works but in one line.
Is there any way I can add 2 lines as text placeholder?
Hi, thanks for this useful tool
Is there a way to set text background color?
And to center the watermark horizontally while defining a vertical offset (for instance, center at bottom of page)?
Thank you.
Maybe I'm missing something, but is there a way to build this for Apple Silicon? I see the Darwin build, but it is not for ARM 64 as far as I can tell. Same goes for the build.sh. No way to build for Darwin + Arm
Image not working for some files
Example: example.pdf
As-Salamu Alaikum,
$ go build -ldflags=-s .
C:\Users\0wner\go\src\github.com\unidoc\unidoc\pdf\model\writer.go:538:12: constant 4294967295 overflows int
How do proceed?
Thanks in advance.
whether can it support chinese ?
Hi, first, thanks for a great utility! It's working great in testing so far.
Is there any scope for adding PDF properties, such as title, copyright, author etc?
It would be nice to have the option to add watermark from a scalable format, like SVG.
whether can support "Song typeface"?
I specify -x -10 -y -10 as position to have watermark to the bottom right but with some pdf documents the tool doesn't respect the position specified and moves the watermark to the top right. Why does this happen?
Hi,
You state that this is open source, but when I compiled this it required a unidoc dependency from Github. Although your built binaries work fine, when I compile it now, a watermark is added to the file from Unidoc and a license error is thrown.
I was trying to introduce a configuration option so that the watermark was only applied to the first page, but now this isn't possible.
Is this something you can revise?
Hello @ajaxray ,
I'm using the command "markpdf "test_text.pdf" "Approved, Not published" "/var/www/html/test_text_out.pdf" -c -s 20.0 -a 45 -o 0.5" for add text as watermark, but the watermark is very dark. I also change opacity 0.1 but in both cases, the watermark looks the same.
test_text.pdf
But in the case of an image it's work fine.
Thank You In advance
I have found that if I have a PDF document that has bookmarks and links for easy navigation, all of them are removed once I mark my PDF using this tool. Hope that a fix could be implemented so the marked PDF will preserve the bookmarks and links it originally contained. Thanks in advanced!
Killed: 9
I have a PDF file that seems to have an invalid media box. The file opens fine using Adobe PDF reader, so I assume there's a standard default being used.
I can't provide the original file since it has sensitive information on it, and when I edit it, it "corrects" for the error.
I do I have a verbose output capture, and I can provide the output PDF.
markpdf bad.pdf "Test" bad_output.pdf --opacity=1 --font-size=12 --offset-x=10 --offset-y=10 -v
2023/01/03 14:30:05 Input PDF: bad.pdf
2023/01/03 14:30:05 No file exists at: Test, assuming Text Watermark.
[DEBUG] parser.go:1530 Pdf version 1.7
[DEBUG] main.go:131 Failed to get page mediabox: Invalid media box
2023/01/03 14:30:05 Page Width : 0
2023/01/03 14:30:05 Page Height : 0
2023/01/03 14:30:05 Paragraph width: 19.450000
2023/01/03 14:30:05 Paragraph height: 10.000000
2023/01/03 14:30:05 Offsets x: 10.000000, y: 10.000000
I'll see if I can get the source code to work and hard-code some values and see if that makes a difference.
Hi,
The package was working fine, until today... Some of the PDF's can't be handled because i get this error message:
Output:\n================\nERROR: Failed to get PageCount of the source file. [File need to be decrypted first] \n\n\nError Output:\n================\n",
The PDF can be openend without password so i dont understand why it tells it has to be decrypted
func drawText_offset(p *creator.Paragraph, c *creator.Creator, offsetX float64, offsetY float64, att *text_attributes) {
// Change to times bold font (default is helvetica).
p.SetFont(getFontByName(font))
p.SetFontSize(att.FontSize)
p.SetPos(offsetX, offsetY)
p.SetColor(creator.ColorRGBFromHex("#" + att.FontColor))
p.SetAngle(att.Angle)
// Encountering problem with tiles and text watermark. Contributions welcome.
//if tiles {
// repeatTiles(p, c)
// return
//}
_ = c.Draw(p)
}
func markPDF(inputPath string, outputPath string, watermark string) error {
debugInfo(fmt.Sprintf("Input PDF: %v", inputPath))
c := creator.New()
var watermarkImg *creator.Image
var para *creator.Paragraph
isImageMark := isImageMark(watermark)
watermarkIsATemplate := isWatermarkATemplate(watermark)
// Read the input pdf file.
f, err := os.Open(inputPath)
fatalIfError(err, fmt.Sprintf("Failed to open the source file. [%s]", err))
defer f.Close()
pdfReader, err := pdf.NewPdfReader(f)
fatalIfError(err, fmt.Sprintf("Failed to parse the source file. [%s]", err))
numPages, err := pdfReader.GetNumPages()
fatalIfError(err, fmt.Sprintf("Failed to get PageCount of the source file. [%s]", err))
// Prepare data to insert into the template.
rec := Recipient{
Pages: numPages,
Filename: filepath.Base(inputPath[:len(inputPath)-len(filepath.Ext(inputPath))]),
}
var t *template.Template
if !isImageMark && watermarkIsATemplate {
t = template.Must(template.New("watermark").Parse(watermark))
}
for i := 0; i < numPages; i++ {
pageNum := i + 1
rec.Page = pageNum
// Read the page.
page, err := pdfReader.GetPage(pageNum)
fatalIfError(err, fmt.Sprintf("Failed to read page from source. [%s]", err))
// Add to creator.
c.AddPage(page)
// Calculate the position on first page
if pageNum == 1 {
debugInfo(fmt.Sprintf("Page Width : %v", c.Context().PageWidth))
debugInfo(fmt.Sprintf("Page Height : %v", c.Context().PageHeight))
}
if isImageMark {
if pageNum == 1 {
watermarkImg, err = creator.NewImageFromFile(watermark)
fatalIfError(err, fmt.Sprintf("Failed to load watermark image. [%s]", err))
adjustImagePosition(watermarkImg, c)
}
drawImage(watermarkImg, c)
}
var need_draw_text = !isImageMark || full
if need_draw_text {
var attr text_attributes
var text = watermark
if full {
attr.Alpha = float64(water_mark_config.Outdevconf.MaskConf.Alpha)
attr.Angle = float64(water_mark_config.Outdevconf.MaskConf.Angle)
attr.FontSize = float64(water_mark_config.Outdevconf.MaskConf.Fontsize)
attr.FontColor = water_mark_config.Outdevconf.MaskConf.Fontcolor[1:]
text = water_mark_config.Outdevconf.MaskText
}
if pageNum == 1 {
para = creator.NewParagraph(text)
adjustTextPosition(para, c)
}
if watermarkIsATemplate {
applyTemplate(t, &rec, para)
}
if full {
var gap_y = c.Context().PageHeight / 2
var gap_x = c.Context().PageWidth / 4
for x := 0; x < int(c.Context().PageWidth); x = x + int(gap_x) {
for y := 0; y < int(c.Context().PageHeight); y = y + int(gap_y) {
if x%2 == 0 {
drawText_offset(para, c, float64(x), float64(y), &attr)
} else {
drawText_offset(para, c, float64(x), float64(y+int(gap_y)/2), &attr)
}
}
}
} else {
drawText(para, c)
}
}
}
err = c.WriteToFile(outputPath)
return err
}
Using "text/template" advance text watermark can be improved adding pages numbering function
@ajaxray Love markpdf, would be great if there was an option for a page range, e.g. page=3, page=1-5 etc. I want to have a watermark only on one page.
From twitter - https://x.com/inkmihq/status/1780104908110168331?s=46
Hi,
I have a security problem running the program. I am using Windows 10 + Avira Free Security v1.1.57.24596.
Steps to reproduce:
markpdf "path/to/source.pdf" "img/logo.png" "path/to/output.pdf"
Will be good if we could have the possibility to use PDF base64 as input or in the output
Hi. Code works on my local but when i deploy it to my linux server i get a "Bus Error". Can't seem to find any more information around what's causing this.
Any ideas?
As the title says, I get an exe of 0 bytes! Strange isn't it.
Hi,
hopelly someone can help because this bug is driving me crazy.
Initially everything worked fine until few weeks ago.
I have updated markpdf with the latest version.
When i use process, the isSuccessful() returns true,
But the file is not modified.
When i use the exact same options as in process but paste this in the terminal.
Then the file is modified correctly.
This is driving me nuts because i can't make it to work with process and cant see any error messages.
Hopelly someone has an answer.
I would like to use a program to add a stamp. The stamp has a permanent dimension and rich content. Can you add the DPI option to the image? The standard is 72 DPI and the stamp is too big. If I give 144 DPI, the image will be two times smaller without losing quality. 300 DPI will add a small image with high resolution.
Hi @ajaxray,
Is it possible to break the text into two lines if the watermark text size is larger than the page size?
Thank You
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.