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 Request]: opacity
option for drawText()
method
#210
Comments
Hello @akaanish! pdf-lib does not currently provide an API for changing text/shape/image opacity. However, I agree that this would be a useful feature to offer. If anybody would like to work on this, I'd be happy to merge a PR implementing this feature. Otherwise I'll look into it when I get some more pressing issues taken care of. |
opacity
option for drawText()
method
I desperately need opacity for svgpaths, how could I help implementing it? I don't know anything about PDFs, I'm more of a WebGL guy, where should I start? |
Hello @soadzoor! I'd suggest taking a look at the PDF spec and how other PDF libraries implement transparency. Here's a link to the transparency implementation in Some sections of the spec I'd recommend taking a look at:
|
Hello @Hopding ! Thank you for all these information. I've started studying this thing, although it feels like drinking from the firehose. It's a huge ocean I've dived into. I'm pretty sure I'd need a deeper understanding of the spec, and your pdf-lib as well, to be able to do this. This is how far I got so far: I've added these to your
Although, as the spec says:
And I believe, this is one that doesn't have its individual operator. Unfortunately, I can't see an example in your project where you use ExtGState, or even dictionaries (like pdf-kit does), so this is where I got stuck. Could you please help? |
@soadzoor I can certainly relate to feeling overwhelmed by PDFs and the spec. There's a ton of information in there. It took me a long time to digest it all, and I'm still learning little bits and pieces here and there. But on the bright side, you don't need to know everything to get started. And, generally speaking, You'll need access to the const pdfDoc = await PDFDocument.create();
const myDict = pdfDoc.context.obj({
Type: 'Foo',
Qux: 21,
Baz: [{ Bar: 45 }],
});
const myDictRef = pdfDoc.context.register(myDict); You may also find the Here's a rough example to show how you might create a dictionary and add it to the const pdfDoc = await PDFDocument.create();
const page = pdfDoc.addPage();
const { Resources } = page.node.normalizedEntries();
const extGState = pdfDoc.context.obj({
GS1: { Type: 'ExtGState', ca: 0.7, CA: 0.5 }
});
// Note: we shouldn't override this if it already exists!
Resources.set(PDFName.of('ExtGState'), extGState);
page.pushOperators(
pushGraphicsState(),
// Push an operator that sets the graphics state to `GS1`
);
page.drawSvg(...);
page.pushOperators(popGraphicsState()); You can use the |
Thank you @Hopding ! I'll try to play around with this on Monday, and will let you know how it goes! Cheers! |
Hey @Hopding , Thank you for the explanations and the examples, you're super helpful! Something is happening with the opacity, but it's not what I'd expect, so I need your help (again, sorry.. :( ) So, it seems, the opacity is changed, but it only affects the "original PDF's" parts, which I load as an arraybuffer. Which is strange, because I change the graphic state only after it's already loaded (I guess..?). Do you have any idea what's happening, and why? My code (rougly) looks like this:
|
@akaanish There are three steps required to change the opacity:
You appear to be doing the first two steps, but not the third. In my example I left a comment where you need to push the operator, though I suppose it wasn't clear enough. It should be placed directly after page.pushOperators(
pushGraphicsState(),
// Replace this comment with an operator that sets the graphics state to `GS1`
);
page.drawSvg(...);
page.pushOperators(popGraphicsState()); Something like this should work (I haven't tested it though): const setGraphicsState = (state: string | PDFName) =>
PDFOperator.of(Ops.SetGraphicsStateParams, [asPDFName(state)]);
...
page.pushOperators(
pushGraphicsState(),
setGraphicsState('GS1')
); |
@Hopding Oh, I missed that, you're right! Now it works, almost as expected! Thank you very much! |
@soadzoor That's probably because the original PDF already has a graphics state called const extGState = pdfDoc.context.obj({
GS1: { Type: 'ExtGState', ca: 0.7, CA: 0.5 }
});
// Note: we shouldn't override this if it already exists!
Resources.set(PDFName.of('ExtGState'), extGState); As I mentioned, my example does not handle all edge cases. There are two such scenarios that can occur here that you'll need to handle:
|
@Hopding I admire you're patience and persistence towards me! You're fantastic ❤️ |
@soadzoor I'm happy to help! If you're able to get it working, I hope you'll consider submitting a PR to add |
@Hopding Sure, I plan to do that! |
I want to draw text with opacity 50% but i can see i cannot pass alpha value in rgb()
function.
I cannot see how i can set opacity value.
Does this library support?
If not, it would be really nice if you consider it.
The text was updated successfully, but these errors were encountered: